今天在封裝輪播圖片控件時(shí), 偶然發(fā)現(xiàn)調(diào)用 scrollRectToVisible: 方法, scrollView 并沒(méi)有滾動(dòng)到指定位置, 經(jīng)過(guò)調(diào)試發(fā)現(xiàn)在設(shè)置 contentSize 時(shí)高度習(xí)慣性設(shè)置為0, 這就是導(dǎo)致滾動(dòng)失效的原因!
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, 0);
當(dāng)我將高度設(shè)置為一個(gè)隨機(jī)的數(shù)值, scrollView 就可以順利滑動(dòng)了:
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, 10);
順帶我將 UIScrollView 文檔中的代理方法調(diào)用順序也研究了一下
- (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. 當(dāng)手動(dòng)拖動(dòng) scrollView 時(shí), 打印結(jié)果如下:
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. 當(dāng)調(diào)用scrollRectToVisible: 滑動(dòng) scrollView 時(shí), 打印結(jié)果如下:
scrollViewDidScroll ->?scrollViewDidEndScrollingAnimation:
(當(dāng)調(diào)用 setContentOffset:animated:時(shí), 也會(huì)觸發(fā)以下方法)
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:]
根據(jù)以上的學(xué)習(xí), 很輕松封裝了一個(gè)圖片的輪播圖: demo 如下:CycleScrollView