2019-11-28 17:41:12 +08:00
|
|
|
/*
|
|
|
|
* Copyright [2019] [Doric.Pub]
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2019-11-26 20:15:16 +08:00
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/11/26.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricSwipeRefreshLayout.h"
|
|
|
|
#import "UIView+Doric.h"
|
|
|
|
#import "DoricLayouts.h"
|
|
|
|
|
|
|
|
@interface DoricSwipeRefreshLayout () <UIScrollViewDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricSwipeRefreshLayout
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
|
|
if (self = [super initWithFrame:frame]) {
|
|
|
|
self.showsHorizontalScrollIndicator = NO;
|
|
|
|
self.showsVerticalScrollIndicator = NO;
|
2019-11-26 21:25:23 +08:00
|
|
|
self.alwaysBounceVertical = YES;
|
2019-11-26 20:15:16 +08:00
|
|
|
self.delegate = self;
|
2019-11-27 18:07:31 +08:00
|
|
|
if (@available(iOS 11, *)) {
|
|
|
|
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
|
|
|
}
|
2019-11-26 20:15:16 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init {
|
|
|
|
if (self = [super init]) {
|
|
|
|
self.showsHorizontalScrollIndicator = NO;
|
|
|
|
self.showsVerticalScrollIndicator = NO;
|
2019-11-26 21:25:23 +08:00
|
|
|
self.alwaysBounceVertical = YES;
|
2019-11-26 20:15:16 +08:00
|
|
|
self.delegate = self;
|
2019-11-27 18:07:31 +08:00
|
|
|
if (@available(iOS 11, *)) {
|
|
|
|
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
|
|
|
}
|
2019-11-26 20:15:16 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:57:38 +08:00
|
|
|
- (CGSize)sizeThatFits:(CGSize)size {
|
|
|
|
if (self.contentView) {
|
2020-04-08 15:38:45 +08:00
|
|
|
[self.contentView.doricLayout apply:size];
|
2020-04-08 11:57:38 +08:00
|
|
|
return self.contentView.frame.size;
|
|
|
|
}
|
|
|
|
return CGSizeZero;
|
2019-12-03 11:39:42 +08:00
|
|
|
}
|
|
|
|
|
2019-11-26 20:15:16 +08:00
|
|
|
- (void)setContentView:(UIView *)contentView {
|
|
|
|
if (_contentView) {
|
|
|
|
[_contentView removeFromSuperview];
|
|
|
|
}
|
|
|
|
_contentView = contentView;
|
|
|
|
[self addSubview:contentView];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setHeaderView:(UIView *)headerView {
|
|
|
|
if (_headerView) {
|
|
|
|
[_headerView removeFromSuperview];
|
|
|
|
}
|
|
|
|
_headerView = headerView;
|
|
|
|
[self addSubview:headerView];
|
|
|
|
self.refreshable = YES;
|
|
|
|
}
|
|
|
|
|
2020-04-08 15:20:38 +08:00
|
|
|
- (void)layoutSubviews {
|
|
|
|
[super layoutSubviews];
|
|
|
|
if (self.contentOffset.y != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.contentSize = self.frame.size;
|
|
|
|
}
|
|
|
|
|
2019-11-26 20:15:16 +08:00
|
|
|
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
|
|
|
|
if (-scrollView.contentOffset.y >= self.headerView.height) {
|
2019-11-27 19:57:43 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
self.refreshing = YES;
|
|
|
|
});
|
2021-09-01 15:44:04 +08:00
|
|
|
} else if (scrollView.contentOffset.y + scrollView.height > scrollView.contentSize.height) {
|
|
|
|
self.bounces = NO;
|
|
|
|
[UIView animateWithDuration:0.2f animations:^{
|
|
|
|
scrollView.contentOffset = (CGPoint) {0, scrollView.contentSize.height - scrollView.height};
|
|
|
|
} completion:^(BOOL finished) {
|
|
|
|
self.bounces = YES;
|
|
|
|
}];
|
2019-11-26 20:15:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
|
|
|
|
if (scrollView.contentOffset.y <= 0) {
|
2021-05-21 18:28:53 +08:00
|
|
|
if ([self.contentView isKindOfClass:UIScrollView.class]
|
|
|
|
&& ((UIScrollView *) self.contentView).contentOffset.y > 0) {
|
|
|
|
scrollView.contentOffset = (CGPoint) {0, 0};
|
|
|
|
} else {
|
|
|
|
[self.swipePullingDelegate setPullingDistance:-scrollView.contentOffset.y];
|
|
|
|
}
|
|
|
|
} else if (scrollView.contentOffset.y + scrollView.height > scrollView.contentSize.height) {
|
|
|
|
if ([self.contentView isKindOfClass:UIScrollView.class]
|
|
|
|
&& ((UIScrollView *) self.contentView).contentOffset.y + self.contentView.height < ((UIScrollView *) self.contentView).contentSize.height) {
|
|
|
|
scrollView.contentOffset = (CGPoint) {0, scrollView.contentSize.height - scrollView.height};
|
|
|
|
}
|
2019-11-26 20:15:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 19:57:43 +08:00
|
|
|
- (void)setContentOffset:(CGPoint)contentOffset {
|
|
|
|
[super setContentOffset:contentOffset];
|
|
|
|
}
|
|
|
|
|
2019-11-26 20:15:16 +08:00
|
|
|
- (void)setRefreshing:(BOOL)refreshing {
|
|
|
|
if (_refreshing == refreshing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (refreshing) {
|
2019-12-03 11:39:42 +08:00
|
|
|
[self setContentOffset:CGPointMake(0, -self.headerView.height) animated:YES];
|
|
|
|
self.scrollEnabled = NO;
|
2019-11-27 13:30:31 +08:00
|
|
|
if (self.onRefreshBlock) {
|
|
|
|
self.onRefreshBlock();
|
|
|
|
}
|
2019-11-26 20:15:16 +08:00
|
|
|
} else {
|
2019-12-03 11:39:42 +08:00
|
|
|
[self setContentOffset:(CGPoint) {0, 0} animated:YES];
|
|
|
|
self.scrollEnabled = YES;
|
2019-11-26 20:15:16 +08:00
|
|
|
}
|
2020-08-19 20:08:14 +08:00
|
|
|
if (refreshing) {
|
|
|
|
[self.swipePullingDelegate startAnimation];
|
|
|
|
} else {
|
|
|
|
[self.swipePullingDelegate stopAnimation];
|
|
|
|
}
|
2019-11-26 20:15:16 +08:00
|
|
|
_refreshing = refreshing;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRefreshable:(BOOL)refreshable {
|
|
|
|
self.scrollEnabled = refreshable;
|
2019-11-27 19:57:43 +08:00
|
|
|
if (refreshable) {
|
|
|
|
self.contentOffset = (CGPoint) {0, 0};
|
|
|
|
self.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
|
|
|
|
}
|
2019-11-26 20:15:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)refreshable {
|
|
|
|
return self.scrollEnabled;
|
|
|
|
}
|
2019-11-26 21:25:23 +08:00
|
|
|
|
|
|
|
- (void)setContentSize:(CGSize)contentSize {
|
|
|
|
[super setContentSize:contentSize];
|
|
|
|
}
|
2019-11-26 20:15:16 +08:00
|
|
|
@end
|