This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/iOS/Pod/Classes/Refresh/DoricSwipeRefreshLayout.m

155 lines
4.3 KiB
Mathematica
Raw Normal View History

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"
#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-11-27 16:04:43 +08:00
- (void)layoutSelf:(CGSize)targetSize {
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) {
[self.swipePullingDelegate setProgressRotation:-scrollView.contentOffset.y / self.headerView.height];
}
}
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) {
if (self.onRefreshBlock) {
self.onRefreshBlock();
}
2019-11-27 19:57:43 +08:00
[UIView animateWithDuration:.3f
2019-11-26 20:15:16 +08:00
animations:^{
2019-11-27 19:57:43 +08:00
self.contentOffset = (CGPoint) {0, -self.headerView.height};
2019-11-26 20:15:16 +08:00
self.contentInset = UIEdgeInsetsMake(self.headerView.height, 0, 0, 0);
}
completion:^(BOOL finished) {
[self.swipePullingDelegate startAnimation];
2019-11-27 19:57:43 +08:00
self.scrollEnabled = NO;
2019-11-26 20:15:16 +08:00
}
];
} else {
2019-11-27 19:57:43 +08:00
self.bounces = YES;
[UIView animateWithDuration:.3f
2019-11-26 20:15:16 +08:00
animations:^{
2019-11-27 19:57:43 +08:00
self.contentOffset = (CGPoint) {0, 0};
2019-11-26 20:15:16 +08:00
self.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
completion:^(BOOL finished) {
[self.swipePullingDelegate stopAnimation];
2019-11-27 19:57:43 +08:00
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