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"
|
2019-11-28 10:28:24 +08:00
|
|
|
#import "Doric.h"
|
2019-11-26 20:15:16 +08:00
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)sizeThatFits:(CGSize)size {
|
|
|
|
if (self.contentView) {
|
2019-11-27 16:04:43 +08:00
|
|
|
return [self.contentView measureSize:size];
|
2019-11-26 20:15:16 +08:00
|
|
|
}
|
|
|
|
return CGSizeZero;
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:33:47 +08:00
|
|
|
- (BOOL)requestFromSubview:(UIView *)subview {
|
|
|
|
if (subview == self.headerView) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
return [super requestFromSubview:subview];
|
|
|
|
}
|
|
|
|
|
2019-12-03 11:39:42 +08:00
|
|
|
- (void)layoutSubviews {
|
|
|
|
[super layoutSubviews];
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:04:43 +08:00
|
|
|
- (void)layoutSelf:(CGSize)targetSize {
|
2019-12-03 09:27:20 +08:00
|
|
|
if (self.contentOffset.y != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-28 10:28:24 +08:00
|
|
|
self.width = targetSize.width;
|
|
|
|
self.height = targetSize.height;
|
|
|
|
[self.headerView also:^(UIView *it) {
|
|
|
|
[it layoutSelf:[it measureSize:targetSize]];
|
|
|
|
it.bottom = 0;
|
|
|
|
it.centerX = self.centerX;
|
|
|
|
}];
|
|
|
|
[self.contentView also:^(UIView *it) {
|
|
|
|
[it layoutSelf:targetSize];
|
|
|
|
}];
|
2019-11-27 17:33:47 +08:00
|
|
|
self.contentSize = self.frame.size;
|
2019-11-27 16:04:43 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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;
|
|
|
|
});
|
2019-11-26 20:15:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
|
|
|
|
if (scrollView.contentOffset.y <= 0) {
|
2019-12-02 21:01:34 +08:00
|
|
|
[self.swipePullingDelegate setPullingDistance:-scrollView.contentOffset.y];
|
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
|
|
|
}
|
|
|
|
_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
|