【问题标题】:Swipe doesn't work properly in a uiscrollview滑动在 uiscrollview 中无法正常工作
【发布时间】:2014-04-21 00:14:48
【问题描述】:

我有一个名为templateViewUISrollView。我必须向它添加一个滑动手势,以允许用户向左/向右滑动以查看另一个模板。问题是大多数时候用户不能轻松滑动,因为视图向下/向上滚动而不是滑动到另一个视图。他的手指需要严格水平对齐才能滑动到另一个页面,从用户体验的角度来看这是不可接受的。

知道如何处理这种情况吗?有没有办法实现检测滑动手势的角度?或者,有没有办法将其作为自定义手势来检测具有特定角度的斜线?

提前致谢。

【问题讨论】:

    标签: ios uiscrollview swipe


    【解决方案1】:

    尝试实现 UIGestureRecognizer Delegate 方法。当gestureRecognizer 或otherGestureRecognizer 对手势的识别会阻止其他手势识别器识别其手势时,将调用此方法。请注意,返回 YES 保证允许同时识别。

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
        shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer  
        {
            return YES;
        }
    

    参考:UIGestureRecognizer Protocol

    在初始化滑动手势时不要忘记分配委托。

    更新 1 创建自己的手势

    您始终可以继承 UIGestureRecognizer 类并实现 touchesBegantouchesMovedtouchesEnded 方法 - 根据您自己的需要手动管理手势的状态。

    我发布了一些实现自定义 EdgeGestureRecognizer 的示例代码,以便您更好地理解。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesBegan:touches withEvent:event];
        UITouch *touch = touches.anyObject;
        CGPoint location = [touches.anyObject locationInView:self.view];
    
        // if not single finger, then fail
    
        if ([touches count] != 1)
        {
            self.state = UIGestureRecognizerStateFailed;
            return;
        }
       //put here some logics for your case. For instance, you can register
       //here your first touch location, it will help
       //you to calculate the angle after.
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesMoved:touches withEvent:event];
    
        if (self.state == UIGestureRecognizerStateFailed) return;
    
        UITouch *touch = touches.anyObject;
        self.previousPoint = self.currentPoint;
        self.previousPointTime = self.currentPointTime;
        self.currentPoint = [touch locationInView:self.view];
        self.currentPointTime = touch.timestamp;
    
        if (self.state == UIGestureRecognizerStatePossible)
        {
            CGPoint translate = CGPointMake(self.currentPoint.x - self.startPoint.x, self.currentPoint.y - self.startPoint.y);
    
            // see if we've moved the necessary minimum distance
    
            if (sqrt(translate.x * translate.x + translate.y * translate.y) >= self.minimumRecognitionDistance)
            {
                // recognize if the angle is roughly horizontal, otherwise fail
    
                double angle = atan2(translate.y, translate.x);
    
                if ([self isAngleCloseEnough:angle])
                    self.state = UIGestureRecognizerStateBegan;
                else
                    self.state = UIGestureRecognizerStateFailed;
            }
        }
        else if (self.state == UIGestureRecognizerStateBegan)
        {
            self.state = UIGestureRecognizerStateChanged;
        }
    }
    

    【讨论】:

    • 其实我已经试过了,并没有改善滑动功能。该问题与同时识别两个手势无关,因为目前它们已被正确识别,但我的要求是如何改进它以使其对用户更敏感,以便他在视图之间滑动时不会遇到任何问题.
    • 还有另一种方法,将您的模板视图构建为另一个具有分页启用模式的水平滚动视图。有很多方法,请在您的问题中更具体。发布一些代码,发布一些屏幕截图,并更详细地说明您想要实现的目标。
    • 我认为还不够清楚。让我备份。这个想法是让滑动功能在 UIScrollView 中顺利运行。我知道我们可以同时支持这两种功能并且它可以正常工作,但是只有当用户的手指严格水平滑动时才能进行滑动。那是我的问题。我正在寻找的是一种增强此功能的方法。例如,我现在尝试在 UIScrollView 中添加对角滑动手势。我希望现在已经足够清楚了。
    • 这几乎就是我所做的。谢谢
    • @Maystro,这就是为什么强烈建议发布一些代码来显示您所做的事情。在这种情况下,帮助您会容易得多。先生,编码愉快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多