今天在封装轮播图片控件时, 偶然发现调用 scrollRectToVisible: 方法, scrollView 并没有滚动到指定位置, 经过调试发现在设置 contentSize 时高度习惯性设置为0, 这就是导致滚动失效的原因!
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, 0);
当我将高度设置为一个随机的数值, scrollView 就可以顺利滑动了:
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, 10);
顺带我将 UIScrollView 文档中的代理方法调用顺序也研究了一下
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0){
NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"%s", __FUNCTION__);
[_timer invalidate];
_timer = nil;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView moving {
NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
NSLog(@"%s", __FUNCTION__);
}
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
NSLog(@"%s", __FUNCTION__);
return scrollView;
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view {
NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale
animations {
NSLog(@"%s", __FUNCTION__);
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
NSLog(@"%s", __FUNCTION__);
return YES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
NSLog(@"%s", __FUNCTION__); }
/* Also see -[UIScrollView adjustedContentInsetDidChange] */
- (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView {
NSLog(@"%s", __FUNCTION__);
}
1. 当手动拖动 scrollView 时, 打印结果如下:
scrollViewWillBeginDragging -> scrollViewDidScroll -> scrollViewWillEndDragging:withVelocity:targetContentOffset: -> scrollViewDidEndDragging:willDecelerate: -> scrollViewWillBeginDecelerating: -> scrollViewDidScroll -> scrollViewDidEndDecelerating
2018-01-18 14:01:09.972615+0800 CircleScrollView[46263:748223] -[ViewController scrollViewWillBeginDragging:]
2018-01-18 14:01:09.994936+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.016610+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.039564+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.062201+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.086289+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.096182+0800 CircleScrollView[46263:748223] -[ViewController scrollViewWillEndDragging:withVelocity:targetContentOffset:]
2018-01-18 14:01:10.096412+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidEndDragging:willDecelerate:]
2018-01-18 14:01:10.096735+0800 CircleScrollView[46263:748223] -[ViewController scrollViewWillBeginDecelerating:]
2018-01-18 14:01:10.114628+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.131218+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.147934+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.164530+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.180361+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.197032+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.213675+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.230596+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:] 2018-01-18 14:01:10.247163+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.263906+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.281192+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.297228+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.313823+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.330661+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.347330+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.363705+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.380227+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.397741+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.414423+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.430846+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidScroll:]
2018-01-18 14:01:10.447037+0800 CircleScrollView[46263:748223] -[ViewController scrollViewDidEndDecelerating:]
2. 当调用scrollRectToVisible: 滑动 scrollView 时, 打印结果如下:
scrollViewDidScroll -> scrollViewDidEndScrollingAnimation:
(当调用 setContentOffset:animated:时, 也会触发以下方法)
2018-01-18 14:13:39.964215+0800 CircleScrollView[48337:868168] -[ViewController scrollViewDidScroll:]
2018-01-18 14:13:41.223869+0800 CircleScrollView[48337:868168] -[ViewController scrollViewDidScroll:]
2018-01-18 14:13:41.929535+0800 CircleScrollView[48337:868168] -[ViewController scrollViewDidScroll:]
2018-01-18 14:13:41.930031+0800 CircleScrollView[48337:868168] -[ViewController scrollViewDidEndScrollingAnimation:]
根据以上的学习, 很轻松封装了一个图片的轮播图: demo 如下:CycleScrollView