如何检测iPhone上左/右滑动? [英] How to detect swiping left / right on the iPhone?

查看:135
本文介绍了如何检测iPhone上左/右滑动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法可以为iPhone检测这些类型的手势?
我可以使用touchesBegan,touchesMoved,touchesEnded。但是我该如何实现手势呢? thz u。

Is there any easy way to detect these kinds of gestures for iPhone? I can use touchesBegan, touchesMoved, touchesEnded. But how can I implement the gestures? thz u.

推荐答案

你走在正确的轨道上。在touchesBegan中,你应该存储用户第一次触摸屏幕的位置。

You are on the right track. In touchesBegan you should store the position where the user first touches the screen.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    self.startPosition = [touch locationInView:self];
}

touchesEnded中的类似代码为您提供最终位置。通过比较这两个位置,您可以确定运动方向。如果x坐标已移动超过某个公差,则可以向左或向右滑动。

Similar code in touchesEnded gives you the final position. By comparing the two positions you can determine the direction of movement. If the x-coordinate has moved beyond a certain tolerance you have a left or right swipe.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject]; 
    CGPoint endPosition = [touch locationInView:self];

    if (startPosition.x < endPosition.x) {
        // Right swipe
    } else {
        // Left swipe
    }
}

您不需要touchesMoved,除非您想在用户检测和跟踪滑动时仍然触摸屏幕。在决定他们执行滑动之前,用户已经移动了最小距离也是值得测试的。

You do not need touchesMoved unless you want to detect and track a swipe while the user is still touching the screen. It may also be worth testing that the user has moved a minimal distance before deciding they have performed a swipe.

这篇关于如何检测iPhone上左/右滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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