實用小技巧(一):UIScrollView中上下左右滾動方向的判斷


https://www.jianshu.com/p/93e8459b6dae

2017.06.01 01:13* 字數 674 閱讀 1201評論 0

2017.06.01 01:13* 字數 674 閱讀 1201評論 0喜歡 1

版本記錄

版本號

時間

V1.0

2017.05.31

前言

ios中又很多實用的小技巧,實現不難很實用,以后我會慢慢的給大家總結。這一篇主要講述滾動方向的判斷。

  滾動視圖是app很常用的視圖形式,而其中scrollView及其子類UITableView和UICollectionView用的很多,在用的時候有一個需求就是:需要判斷當前的滾動方向

  • 向上滾動
  • 向下滾動
  • 向左滾動
  • 向右滾動
  • 下面就以兩個例子對滾動方向進行判斷,下面我們開始。

詳情

一、左右滾動方向的判斷

下面我們就以UIScrollView為例子,說明左右滾動是如何判斷的,還是先看一下代碼組成。

 

pastedGraphic.png

代碼組成

思路引導

下面我們看,當我們向左滾動時可見,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為例子,說明上下滾動是如何判斷的,還是先看一下代碼組成。

 

pastedGraphic_1.png

代碼組織

思路引導

  前面有了左右方向的判斷,相信大家掌握了,那么上下方向的判斷也是這個思路。

先看向上滾動的輸出

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 

版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM