• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 滑动在 uiscrollview 中无法正常工作

[复制链接]
菜鸟教程小白 发表于 2022-12-13 06:53:57 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

提前致谢。



Best Answer-推荐答案


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

- (BOOL)gestureRecognizerUIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizerUIGestureRecognizer *)otherGestureRecognizer  
    {
        return YES;
    }

引用: UIGestureRecognizer Protocol

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

更新 1 创建自己的手势

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

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

- (void)touchesBeganNSSet *)touches withEventUIEvent *)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)touchesMovedNSSet *)touches withEventUIEvent *)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;
    }
}

关于ios - 滑动在 uiscrollview 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426447/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap