补一下这个文章的资源,我只后有找到了两种其他人的实现,加上我的实现一共三种方式(我的最垃圾了,别在意这些细节)然后放在了https://github.com/sweetKnight/ScrollerViewPage这个地址里。
还是先说说,具体的情况是怎么样的,
刚刚开始的样子,然后要求一滑动就滑动到第二个图
第一眼看到这个要求,我就想到了pagingEnabled属性,但是pagingEnabled属性是一滚动一屏幕并不是一个特定的距离,然后查了一下发现也不能改,好吧屈服了,自己用rollerView的滑动代理自己做了个这样的效果。还不错,遇见这种需求的可以看看,
实现下面的代理
//系统动画停止是刷新当前偏移量_offer是我定义的全局变量
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
_offer = scrollView.contentOffset.x;
NSLog(@"end========%f",_offer);
}
//滑动减速是触发的代理,当用户用力滑动或者清扫时触发
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
if (fabs(scrollView.contentOffset.x -_offer) > 10) {
if (scrollView.contentOffset.x > _offer) {
int i = scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width - 30)+1;
NSIndexPath * index = [NSIndexPath indexPathForRow:i inSection:0];
[_collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}else{
int i = scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width - 30)+1;
NSIndexPath * index = [NSIndexPath indexPathForRow:i-1 inSection:0];
[_collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
}
}
//用户拖拽是调用
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if (fabs(scrollView.contentOffset.x -_offer) > 20) {
if (scrollView.contentOffset.x > _offer) {
int i = scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width - 30)+1;
NSIndexPath * index = [NSIndexPath indexPathForRow:i inSection:0];
[_collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}else{
int i = scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width - 30)+1;
NSIndexPath * index = [NSIndexPath indexPathForRow:i-1 inSection:0];
[_collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
}
}
写完这些你可能发现效果不好,原因是系统的减速点太大了我们改小点就好了
_collectionView.decelerationRate = 10;//我改的是10