在UIScrollView中查找滚动方向? [英] Finding the direction of scrolling in a UIScrollView?

查看:78
本文介绍了在UIScrollView中查找滚动方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIScrollView 只允许水平滚动,我想知道用户滚动的方向(左,右)。我做的是子类化 UIScrollView 并覆盖 touchesMoved 方法:

I have a UIScrollView with only horizontal scrolling allowed, and I would like to know which direction (left, right) the user scrolls. What I did was to subclass the UIScrollView and override the touchesMoved method:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    float now = [touch locationInView:self].x;
    float before = [touch previousLocationInView:self].x;
    NSLog(@"%f %f", before, now);
    if (now > before){
        right = NO;
        NSLog(@"LEFT");
    }
    else{
        right = YES;
        NSLog(@"RIGHT");

    }

}

有时候不会得到调用,当我移动。

But this method sometimes doesn't get called at all when I move. What do you think?

推荐答案

确定方向是相当直接的,但请记住,方向可以改变几次一个手势的过程。例如,如果你有一个scrollview打开分页,用户滑动到下一页,初始方向可能是向右的,但如果你有反弹打开,它会短暂地没有方向,然后,然后

Determining the direction is fairly straightforward, but keep in mind that the direction can change several times over the course of a gesture. For example, if you have a scrollview with paging turned on and the user swipes to go to the next page, the initial direction could be rightward, but if you have bounce turned on, it will briefly be going in no direction at all and then briefly be going leftward.

要确定方向,您需要使用 UIScrollView scrollViewDidScroll 委托。在这个示例中,我创建了一个名为 lastContentOffset 的变量,用于将当前内容偏移量与上一个偏移量进行比较。如果它更大,那么scrollView正在向右滚动。如果小于scrollView,则向左滚动:

To determine the direction, you'll need to use the UIScrollView scrollViewDidScroll delegate. In this sample, I created a variable named lastContentOffset which I use to compare the current content offset with the previous one. If it's greater, then the scrollView is scrolling right. If it's less then the scrollView is scrolling left:

//somewhere in the header
@property (nonatomic, assign) CGFloat lastContentOffset
//
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   ScrollDirection scrollDirection;
   if (self.lastContentOffset > scrollView.contentOffset.x)
      scrollDirection = ScrollDirectionRight;
   else if (self.lastContentOffset < scrollView.contentOffset.x) 
      scrollDirection = ScrollDirectionLeft;

   self.lastContentOffset = scrollView.contentOffset.x;

   // do whatever you need to with scrollDirection here.    
}

我使用下面的枚举来定义方向。将第一个值设置为ScrollDirectionNone具有在初始化变量时将该方向设置为默认值的额外好处:

I'm using the following enum to define direction. Setting the first value to ScrollDirectionNone has the added benefit of making that direction the default when initializing variables:

typedef enum ScrollDirection {
    ScrollDirectionNone,
    ScrollDirectionRight,
    ScrollDirectionLeft,
    ScrollDirectionUp,
    ScrollDirectionDown,
    ScrollDirectionCrazy,
} ScrollDirection;

这篇关于在UIScrollView中查找滚动方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆