实用小技巧(一):UIScrollView中上下左右滚动方向的判断
https://www.jianshu.com/p/93e8459b6dae
2017.06.01 01:13* 字数 674 阅读 1201评论 0喜欢 1
版本记录
版本号 |
时间 |
V1.0 |
2017.05.31 |
前言
ios中又很多实用的小技巧,实现不难很实用,以后我会慢慢的给大家总结。这一篇主要讲述滚动方向的判断。
滚动视图是app很常用的视图形式,而其中scrollView及其子类UITableView和UICollectionView用的很多,在用的时候有一个需求就是:需要判断当前的滚动方向
- 向上滚动
- 向下滚动
- 向左滚动
- 向右滚动
- 下面就以两个例子对滚动方向进行判断,下面我们开始。
详情
一、左右滚动方向的判断
下面我们就以UIScrollView为例子,说明左右滚动是如何判断的,还是先看一下代码组成。
代码组成
思路引导
下面我们看,当我们向左滚动时可见,x为正值且越来越大,如下:
2017-06-01 00:15:47.962 scrollDirectionJudge[2981:77203] x==0.333333,y==-64.000000
2017-06-01 00:15:48.026 scrollDirectionJudge[2981:77203] x==1.000000,y==-64.000000
2017-06-01 00:15:48.057 scrollDirectionJudge[2981:77203] x==1.333333,y==-64.000000
2017-06-01 00:15:48.089 scrollDirectionJudge[2981:77203] x==1.666667,y==-64.000000
2017-06-01 00:15:48.122 scrollDirectionJudge[2981:77203] x==2.000000,y==-64.000000
2017-06-01 00:15:48.145 scrollDirectionJudge[2981:77203] x==4.000000,y==-64.000000
2017-06-01 00:15:48.170 scrollDirectionJudge[2981:77203] x==7.666667,y==-64.000000
2017-06-01 00:15:48.194 scrollDirectionJudge[2981:77203] x==11.000000,y==-64.000000
2017-06-01 00:15:48.218 scrollDirectionJudge[2981:77203] x==12.666667,y==-64.000000
2017-06-01 00:15:48.235 scrollDirectionJudge[2981:77203] x==13.000000,y==-64.000000
2017-06-01 00:15:48.956 scrollDirectionJudge[2981:77203] x==13.666667,y==-64.000000
2017-06-01 00:15:49.036 scrollDirectionJudge[2981:77203] x==14.000000,y==-64.000000
反过来,如果我们向右滑动,可见
2017-06-01 00:17:27.177 scrollDirectionJudge[2981:77203] x==412.000000,y==-64.000000
2017-06-01 00:17:27.200 scrollDirectionJudge[2981:77203] x==407.666667,y==-64.000000
2017-06-01 00:17:27.216 scrollDirectionJudge[2981:77203] x==405.333333,y==-64.000000
2017-06-01 00:17:27.240 scrollDirectionJudge[2981:77203] x==402.333333,y==-64.000000
2017-06-01 00:17:27.265 scrollDirectionJudge[2981:77203] x==401.333333,y==-64.000000
2017-06-01 00:17:27.288 scrollDirectionJudge[2981:77203] x==401.000000,y==-64.000000
2017-06-01 00:17:27.353 scrollDirectionJudge[2981:77203] x==400.666667,y==-64.000000
2017-06-01 00:17:27.377 scrollDirectionJudge[2981:77203] x==399.666667,y==-64.000000
2017-06-01 00:17:27.400 scrollDirectionJudge[2981:77203] x==397.666667,y==-64.000000
2017-06-01 00:17:27.416 scrollDirectionJudge[2981:77203] x==395.666667,y==-64.000000
2017-06-01 00:17:27.432 scrollDirectionJudge[2981:77203] x==393.333333,y==-64.000000
2017-06-01 00:17:27.448 scrollDirectionJudge[2981:77203] x==391.666667,y==-64.000000
2017-06-01 00:17:27.473 scrollDirectionJudge[2981:77203] x==390.333333,y==-64.000000
那么,我们可见,可以有这个思路,如果我们记录当前的x偏移值,并且用下一时刻临近的x偏移值与之相比较,取差值,如果下一时刻的x偏移值 - 当前x偏移值为正值,则是向左边滑动,反之则为向右滑动。
看输出结果,先看向左滑动
2017-06-01 00:24:38.629 scrollDirectionJudge[3185:84739] x==4.666667,y==-64.000000
2017-06-01 00:24:38.629 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.652 scrollDirectionJudge[3185:84739] x==10.666667,y==-64.000000
2017-06-01 00:24:38.652 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.676 scrollDirectionJudge[3185:84739] x==15.666667,y==-64.000000
2017-06-01 00:24:38.677 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.700 scrollDirectionJudge[3185:84739] x==20.000000,y==-64.000000
2017-06-01 00:24:38.700 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.726 scrollDirectionJudge[3185:84739] x==22.666667,y==-64.000000
2017-06-01 00:24:38.727 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.747 scrollDirectionJudge[3185:84739] x==24.333333,y==-64.000000
2017-06-01 00:24:38.748 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.764 scrollDirectionJudge[3185:84739] x==25.000000,y==-64.000000
2017-06-01 00:24:38.765 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.792 scrollDirectionJudge[3185:84739] x==25.666667,y==-64.000000
2017-06-01 00:24:38.792 scrollDirectionJudge[3185:84739] 正在向左滑动
在看向右滑动
2017-06-01 00:25:58.949 scrollDirectionJudge[3185:84739] x==27.333333,y==-64.000000
2017-06-01 00:25:58.950 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:58.970 scrollDirectionJudge[3185:84739] x==24.333333,y==-64.000000
2017-06-01 00:25:58.970 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:58.986 scrollDirectionJudge[3185:84739] x==22.666667,y==-64.000000
2017-06-01 00:25:58.986 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.002 scrollDirectionJudge[3185:84739] x==21.333333,y==-64.000000
2017-06-01 00:25:59.003 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.018 scrollDirectionJudge[3185:84739] x==20.000000,y==-64.000000
2017-06-01 00:25:59.019 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.034 scrollDirectionJudge[3185:84739] x==19.333333,y==-64.000000
2017-06-01 00:25:59.035 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.054 scrollDirectionJudge[3185:84739] x==18.666667,y==-64.000000
2017-06-01 00:25:59.054 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.074 scrollDirectionJudge[3185:84739] x==16.000000,y==-64.000000
2017-06-01 00:25:59.074 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.090 scrollDirectionJudge[3185:84739] x==14.000000,y==-64.000000
2017-06-01 00:25:59.091 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.115 scrollDirectionJudge[3185:84739] x==11.666667,y==-64.000000
2017-06-01 00:25:59.115 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.138 scrollDirectionJudge[3185:84739] x==10.666667,y==-64.000000
2017-06-01 00:25:59.139 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.195 scrollDirectionJudge[3185:84739] x==10.000000,y==-64.000000
2017-06-01 00:25:59.196 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.251 scrollDirectionJudge[3185:84739] x==9.666667,y==-64.000000
2017-06-01 00:25:59.251 scrollDirectionJudge[3185:84739] 正在向右滑动
代码实现
下面我们就直接看代码的实现。
JJHorizontalDirectionVC.m
#import "JJHorizontalDirectionVC.h"
@interface JJHorizontalDirectionVC () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *textScrollView;
@property (nonatomic, assign) CGFloat lastOffSetX;
@end
@implementation JJHorizontalDirectionVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupUI];
}
#pragma mark - Object Private Function
- (void)setupUI
{
UIScrollView *textScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
textScrollView.backgroundColor = [UIColor greenColor];
textScrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, 0);
textScrollView.delegate = self;
[self.view addSubview:textScrollView];
self.textScrollView = textScrollView;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"x==%f,y==%f",scrollView.contentOffset.x,scrollView.contentOffset.y);
if (scrollView.contentOffset.x - self.lastOffSetX > 0) {
NSLog(@"正在向左滑动");
}
else {
NSLog(@"正在向右滑动");
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.lastOffSetX = scrollView.contentOffset.x;
}
@end
二、上下滚动方向的判断
下面我们就以UIScrollView为例子,说明上下滚动是如何判断的,还是先看一下代码组成。
代码组织
思路引导
前面有了左右方向的判断,相信大家掌握了,那么上下方向的判断也是这个思路。
先看向上滚动的输出
2017-06-01 00:48:03.131 scrollDirectionJudge[3579:100464] x==0.000000,y==0.666667
2017-06-01 00:48:03.148 scrollDirectionJudge[3579:100464] x==0.000000,y==2.000000
2017-06-01 00:48:03.171 scrollDirectionJudge[3579:100464] x==0.000000,y==3.666667
2017-06-01 00:48:03.187 scrollDirectionJudge[3579:100464] x==0.000000,y==5.000000
2017-06-01 00:48:03.203 scrollDirectionJudge[3579:100464] x==0.000000,y==6.000000
2017-06-01 00:48:03.229 scrollDirectionJudge[3579:100464] x==0.000000,y==6.666667
2017-06-01 00:48:03.246 scrollDirectionJudge[3579:100464] x==0.000000,y==7.000000
2017-06-01 00:48:03.733 scrollDirectionJudge[3579:100464] x==0.000000,y==7.333333
2017-06-01 00:48:03.757 scrollDirectionJudge[3579:100464] x==0.000000,y==8.000000
2017-06-01 00:48:03.773 scrollDirectionJudge[3579:100464] x==0.000000,y==8.333333
2017-06-01 00:48:03.797 scrollDirectionJudge[3579:100464] x==0.000000,y==9.333333
2017-06-01 00:48:03.832 scrollDirectionJudge[3579:100464] x==0.000000,y==10.333333
2017-06-01 00:48:03.863 scrollDirectionJudge[3579:100464] x==0.000000,y==11.000000
2017-06-01 00:48:03.981 scrollDirectionJudge[3579:100464] x==0.000000,y==11.333333
2017-06-01 00:48:04.005 scrollDirectionJudge[3579:100464] x==0.000000,y==12.666667
2017-06-01 00:48:04.029 scrollDirectionJudge[3579:100464] x==0.000000,y==14.333333
2017-06-01 00:48:04.047 scrollDirectionJudge[3579:100464] x==0.000000,y==15.000000
2017-06-01 00:48:04.064 scrollDirectionJudge[3579:100464] x==0.000000,y==15.333333
2017-06-01 00:48:04.214 scrollDirectionJudge[3579:100464] x==0.000000,y==16.000000
2017-06-01 00:48:04.237 scrollDirectionJudge[3579:100464] x==0.000000,y==17.000000
2017-06-01 00:48:04.262 scrollDirectionJudge[3579:100464] x==0.000000,y==19.333333
2017-06-01 00:48:04.286 scrollDirectionJudge[3579:100464] x==0.000000,y==21.666667
2017-06-01 00:48:04.311 scrollDirectionJudge[3579:100464] x==0.000000,y==22.666667
2017-06-01 00:48:04.494 scrollDirectionJudge[3579:100464] x==0.000000,y==23.666667
2017-06-01 00:48:04.518 scrollDirectionJudge[3579:100464] x==0.000000,y==24.666667
我们在看一下向下滚动
2017-06-01 00:49:12.548 scrollDirectionJudge[3579:100464] x==0.000000,y==735.333333
2017-06-01 00:49:12.571 scrollDirectionJudge[3579:100464] x==0.000000,y==733.333333
2017-06-01 00:49:12.587 scrollDirectionJudge[3579:100464] x==0.000000,y==732.666667
2017-06-01 00:49:12.614 scrollDirectionJudge[3579:100464] x==0.000000,y==732.000000
2017-06-01 00:49:12.660 scrollDirectionJudge[3579:100464] x==0.000000,y==731.666667
2017-06-01 00:49:12.796 scrollDirectionJudge[3579:100464] x==0.000000,y==731.333333
2017-06-01 00:49:12.813 scrollDirectionJudge[3579:100464] x==0.000000,y==730.666667
2017-06-01 00:49:12.831 scrollDirectionJudge[3579:100464] x==0.000000,y==730.000000
2017-06-01 00:49:13.101 scrollDirectionJudge[3579:100464] x==0.000000,y==729.666667
2017-06-01 00:49:13.125 scrollDirectionJudge[3579:100464] x==0.000000,y==729.000000
2017-06-01 00:49:13.149 scrollDirectionJudge[3579:100464] x==0.000000,y==725.666667
2017-06-01 00:49:13.167 scrollDirectionJudge[3579:100464] x==0.000000,y==721.333333
2017-06-01 00:49:13.183 scrollDirectionJudge[3579:100464] x==0.000000,y==717.000000
2017-06-01 00:49:13.204 scrollDirectionJudge[3579:100464] x==0.000000,y==711.000000
2017-06-01 00:49:13.221 scrollDirectionJudge[3579:100464] x==0.000000,y==709.000000
2017-06-01 00:49:13.237 scrollDirectionJudge[3579:100464] x==0.000000,y==707.333333
2017-06-01 00:49:13.265 scrollDirectionJudge[3579:100464] x==0.000000,y==706.333333
那么,我们可见,可以有这个思路,如果我们记录当前的y偏移值,并且用下一时刻临近的y偏移值与之相比较,取差值,如果下一时刻的y偏移值 - 当前y偏移值为正值,则是向上边滑动,反之则为向下边滑动。
下面看最后的输出结果,先看向上滑动。
2017-06-01 00:55:03.163 scrollDirectionJudge[3709:105104] x==0.000000,y==139.666667
2017-06-01 00:55:03.163 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.184 scrollDirectionJudge[3709:105104] x==0.000000,y==143.000000
2017-06-01 00:55:03.184 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.213 scrollDirectionJudge[3709:105104] x==0.000000,y==146.666667
2017-06-01 00:55:03.213 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.232 scrollDirectionJudge[3709:105104] x==0.000000,y==149.666667
2017-06-01 00:55:03.232 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.248 scrollDirectionJudge[3709:105104] x==0.000000,y==151.666667
2017-06-01 00:55:03.248 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.264 scrollDirectionJudge[3709:105104] x==0.000000,y==153.666667
2017-06-01 00:55:03.265 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.297 scrollDirectionJudge[3709:105104] x==0.000000,y==155.333333
2017-06-01 00:55:03.297 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.332 scrollDirectionJudge[3709:105104] x==0.000000,y==156.666667
2017-06-01 00:55:03.332 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.401 scrollDirectionJudge[3709:105104] x==0.000000,y==157.000000
2017-06-01 00:55:03.401 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.457 scrollDirectionJudge[3709:105104] x==0.000000,y==157.333333
2017-06-01 00:55:03.457 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.481 scrollDirectionJudge[3709:105104] x==0.000000,y==158.000000
2017-06-01 00:55:03.481 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.504 scrollDirectionJudge[3709:105104] x==0.000000,y==158.333333
2017-06-01 00:55:03.505 scrollDirectionJudge[3709:105104] 正在向上滑动
2017-06-01 00:55:03.561 scrollDirectionJudge[3709:105104] x==0.000000,y==159.000000
2017-06-01 00:55:03.561 scrollDirectionJudge[3709:105104] 正在向上滑动
在看一下向下滑动
2017-06-01 00:55:30.613 scrollDirectionJudge[3709:105104] x==0.000000,y==734.000000
2017-06-01 00:55:30.614 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.636 scrollDirectionJudge[3709:105104] x==0.000000,y==730.666667
2017-06-01 00:55:30.637 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.653 scrollDirectionJudge[3709:105104] x==0.000000,y==728.666667
2017-06-01 00:55:30.653 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.680 scrollDirectionJudge[3709:105104] x==0.000000,y==723.666667
2017-06-01 00:55:30.680 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.701 scrollDirectionJudge[3709:105104] x==0.000000,y==717.666667
2017-06-01 00:55:30.702 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.731 scrollDirectionJudge[3709:105104] x==0.000000,y==711.000000
2017-06-01 00:55:30.732 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.749 scrollDirectionJudge[3709:105104] x==0.000000,y==705.333333
2017-06-01 00:55:30.749 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.766 scrollDirectionJudge[3709:105104] x==0.000000,y==701.666667
2017-06-01 00:55:30.767 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.790 scrollDirectionJudge[3709:105104] x==0.000000,y==698.666667
2017-06-01 00:55:30.790 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.813 scrollDirectionJudge[3709:105104] x==0.000000,y==697.000000
2017-06-01 00:55:30.813 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.830 scrollDirectionJudge[3709:105104] x==0.000000,y==696.333333
2017-06-01 00:55:30.830 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.926 scrollDirectionJudge[3709:105104] x==0.000000,y==695.666667
2017-06-01 00:55:30.927 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.950 scrollDirectionJudge[3709:105104] x==0.000000,y==694.666667
2017-06-01 00:55:30.951 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.973 scrollDirectionJudge[3709:105104] x==0.000000,y==692.333333
2017-06-01 00:55:30.974 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:30.997 scrollDirectionJudge[3709:105104] x==0.000000,y==690.000000
2017-06-01 00:55:30.998 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:31.014 scrollDirectionJudge[3709:105104] x==0.000000,y==688.666667
2017-06-01 00:55:31.015 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:31.030 scrollDirectionJudge[3709:105104] x==0.000000,y==687.000000
2017-06-01 00:55:31.031 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:31.053 scrollDirectionJudge[3709:105104] x==0.000000,y==684.666667
2017-06-01 00:55:31.054 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:31.069 scrollDirectionJudge[3709:105104] x==0.000000,y==683.666667
2017-06-01 00:55:31.070 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:31.097 scrollDirectionJudge[3709:105104] x==0.000000,y==682.666667
2017-06-01 00:55:31.097 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:31.190 scrollDirectionJudge[3709:105104] x==0.000000,y==681.666667
2017-06-01 00:55:31.191 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:31.287 scrollDirectionJudge[3709:105104] x==0.000000,y==681.333333
2017-06-01 00:55:31.287 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:31.318 scrollDirectionJudge[3709:105104] x==0.000000,y==681.000000
2017-06-01 00:55:31.319 scrollDirectionJudge[3709:105104] 正在向下滑动
2017-06-01 00:55:31.361 scrollDirectionJudge[3709:105104] x==0.000000,y==680.333333
2017-06-01 00:55:31.361 scrollDirectionJudge[3709:105104] 正在向下滑动
代码实现
看代码实现
JJVerticalDirectionVC.m
#import "JJVerticalDirectionVC.h"
@interface JJVerticalDirectionVC () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *textScrollView;
@property (nonatomic, assign) CGFloat lastOffSetY;
@end
@implementation JJVerticalDirectionVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupUI];
}
#pragma mark - Object Private Function
- (void)setupUI
{
UIScrollView *textScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
textScrollView.backgroundColor = [UIColor magentaColor];
textScrollView.contentSize = CGSizeMake(0, self.view.bounds.size.height * 2);
textScrollView.delegate = self;
[self.view addSubview:textScrollView];
self.textScrollView = textScrollView;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"x==%f,y==%f",scrollView.contentOffset.x,scrollView.contentOffset.y);
if (scrollView.contentOffset.y - self.lastOffSetY > 0) {
NSLog(@"正在向上滑动");
}
else {
NSLog(@"正在向下滑动");
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.lastOffSetY = scrollView.contentOffset.y;
}
@end
后记
判断视图的滚动方向是我们经常有的需求,这篇就先介绍到这,后面还会介绍其他的实用小技巧,希望对大家有所帮助,谢谢大家。
UIScrollView如何判断是向右滚动还是向左滚动
https://blog.csdn.net/www9500net_/article/details/52622815
在做类似网易新闻和今日头条的那个效果时候
当我滚动下标为0的时候,也就是第一个标题的时候,往右继续滑动,会重复请求第一个标题的数据
同理当我滚动到最后一个标题的时候,会重复请求最后一个标题的数据
特此记录下解决方法:
1:首先设置pageEnable=YES;
然后通过Delegate实现相关方法。
2:
#pragma mark - 将要开始拖拽,手指已经放在view上并准备拖动的那一刻
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ //拖动前的起始坐标
startContentOffsetX = scrollView.contentOffset.x;
}
3:
#pragma mark - 将要停止前的坐标
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
willEndContentOffsetX = scrollView.contentOffset.x;
}
4:
#pragma mark - 减速停止(必须得有快速拖动的动作,scrollView滚动完毕(速度减为0)并且手已经松开的时候调用)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
endContentOffsetX = scrollView.contentOffset.x;
if (endContentOffsetX < willEndContentOffsetX && willEndContentOffsetX < startContentOffsetX) { //画面从右往左移动,前一页
} else if (endContentOffsetX > willEndContentOffsetX && willEndContentOffsetX > startContentOffsetX) {//画面从左往右移动,后一页
}
}
---------------------
作者:正能量_
来源:CSDN
原文:https://blog.csdn.net/www9500net_/article/details/52622815
版权声明:本文为博主原创文章,转载请附上博文链接!