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

170 lines
4.9 KiB
Mathematica
Raw Normal View History

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"
#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) {
2019-11-29 15:16:15 +08:00
[self.swipePullingDelegate setProgressRotation:-scrollView.contentOffset.y / self.headerView.height * 2];
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) {
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