WWViewScrollObserve
您可以在滚动视图停止滚动时获取视图的可见矩形并观察它是否正在屏幕上显示。监听 UIKit 中所有具有动画的滚动方法。
- 滚动开始触发方法
// delegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
// instance method: only animated == YES
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;
- 滚动结束触发方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
setContentOffset:
和其他没有滚动动画的方法(如 setContentOffset:animated:
和 scrollRectToVisible:animated:
,参数 animated 为 NO)不会监听,您可以使用 [scrollView ww_postScrollBegin]
来通知视图正在滚动,并使用 [scrollView ww_postScrollEnd]
来通知视图已结束滚动。
使用
添加观察
[scrollView ww_registerObserve];
服务操作
view.ww_visibleHandler = ^(CGRect viewRect, CGRect containerRect, BOOL stop) {
NSString *stopStr = stop ? @"🎈End" : @"🌲Begin";
NSString *str = [NSString stringWithFormat:@"%zd-%@", self.index, stopStr];
NSLog(@"%@Scroll\nViewRect:%@, \nContainerRect:%@", str, NSStringFromCGRect(viewRect), NSStringFromCGRect(containerRect));
};
移除监听
[scrollView ww_unregisterObserveWithHandler:^(BOOL isRegistedBefore) {
// need to do something when unregister
}];
安装
- CocoaPods 安装:
pod 'WWViewScrollObserve'
- 下载 ZIP 包,将
WWViewScrollObserve
资源文件拖到工程中。
其他
本库依赖三方库 ReactiveObjC
。在使用前,您可以查看示例程序 WWViewScrollObserveDemo