标签:round nslog logs sig imageview ict nonatomic 结束 自定义
1.上拉刷新
@property(nonatomic,strong)UIImageView *cloudImageView2; @property(nonatomic,strong)UIImageView *cloudImageView3; ..... @property(nonatomic,assign)CGFloat progress;//进度 @property(nonatomic,assign)BOOL isRefreshing;//是否正在刷新 @property(nonatomic,weak )UIScrollView *scrollView;//注意:这里要用weak
@property(nonatomic,copy )Ly_heardRefresh ly_heardRefresh;
+ (instancetype)heardRefreshWithBlock:(Ly_heardRefresh)heardRefresh { LyRefreshView *heard = [[LyRefreshView alloc] init]; heard.ly_heardRefresh = heardRefresh; return heard; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.progress = 0.0; self.isRefreshing = NO; //设置默认属性,添加子控件 } return self; }
- (void)dealloc { if (self.scrollView) { [self.scrollView removeObserver:self forKeyPath:@"contentOffset"]; } }
- (void)willMoveToSuperview:(UIView *)newSuperview { [super willMoveToSuperview:newSuperview]; if (!newSuperview) { [self.superview removeObserver:self forKeyPath:@"contentOffset"]; } } - (void)didMoveToSuperview { [super didMoveToSuperview]; UIView *superView = self.superview; if ([superView isKindOfClass:[UIScrollView class]]) { self.scrollView = (UIScrollView *)superView; } }
- (void)setScrollView:(UIScrollView *)scrollView { _scrollView = scrollView; [self setupImageView:scrollView]; } - (void)setupImageView:(UIScrollView *)scrollView { self.frame = CGRectMake(0, -200, scrollView.bounds.size.width, 200); // [scrollView insertSubview:self atIndex:0]; [scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil]; ..... }
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { if (self.isRefreshing) { return; } if (object == self.scrollView && [keyPath isEqualToString:@"contentOffset"]) { if (self.scrollView.decelerating && self.progress >= 1.0)//停止了拖拽,但还在滑动 { NSLog(@"停止了拖拽,但还在滑动"); //开始刷新,开始刷新做的一些操作 [self beginRefreshing]; if (self.ly_heardRefresh) { self.ly_heardRefresh(); } if ([self.delegate respondsToSelector:@selector(lyRefreshViewWithHeardRefreshing:)]) { [self.delegate lyRefreshViewWithHeardRefreshing:self]; } return; } NSLog(@"将要刷新"); //1.先拿到刷新视图可见区域的高度 CGFloat h = MAX(0, -self.scrollView.contentOffset.y - self.scrollView.contentInset.top); //2.求进度 self.progress = MIN(1, h / kSceneHeight); //3.更新视图 [self updateBackgroundColor]; } }
- (void)beginRefreshing { self.isRefreshing = YES; [self updateRefreshItemPositions]; //停滞,显示刷新视图 [UIView animateWithDuration:0.4 animations:^{ UIEdgeInsets edge = self.scrollView.contentInset; edge.top += kSceneHeight; self.scrollView.contentInset = edge; } completion:^(BOOL finished) { }]; ...动画什么的 }
- (void)endRefreshing { [UIView animateWithDuration:0.4 animations:^{ UIEdgeInsets edge = self.scrollView.contentInset; edge.top -= kSceneHeight; self.scrollView.contentInset = edge; } completion:^(BOOL finished) { self.isRefreshing = NO; }]; ...刷新的时候改变的东西,最后别忘了还原 }
标签:round nslog logs sig imageview ict nonatomic 结束 自定义
原文地址:http://www.cnblogs.com/zhangjie579/p/6753000.html