iPhone 处理用户的滑动

发布于 2024-10-06 22:55:04 字数 44 浏览 4 评论 0 原文

这里有一个简单的问题:当用户在 iPhone 屏幕上滑动手指时,如何检测?

Simple question here: How can I detect when I user swipes their finger on the screen of the iPhone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长安忆 2024-10-13 22:55:04

您需要在应用程序中实现手势识别器。

在您的界面中:

#define kMinimumGestureLength  30
#define kMaximumVariance   5
#import <UIKit/UIKit.h>
@interface *yourView* : UIViewController {
    CGPoint gestureStartPoint;
}
@end

kMinimumGestureLength 是手指在算作滑动之前移动的最小距离。 kMaximumVariance 是手指可以在 y 轴上的起始点上方结束的最大距离(以像素为单位)。

现在打开您的界面.xib 文件并在IB 中选择您的视图,并确保在View Attributes 中启用Multiple Touch

在您的实现中,实施这些方法。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        gestureStartPoint = [touch locationInView:self.view];
  }

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];    

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);


  if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
      //do something 
 }
else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){
      //do something
   }
 }

这是实现滑动识别器的一种方法。另外,您确实应该查看有关此主题的文档:

UISwipeGestureRecognizer

You need to implement a gesture recognizer in your application.

In your interface:

#define kMinimumGestureLength  30
#define kMaximumVariance   5
#import <UIKit/UIKit.h>
@interface *yourView* : UIViewController {
    CGPoint gestureStartPoint;
}
@end

kMinimumGestureLength is the minimum distance the finger as to travel before it counts as a swipe. kMaximumVariance is the maximum distance, in pixels, that the finger can end above the beginning point on the y-axis.

Now open your interface .xib file and select your view in IB, and make sure Multiple Touch is enabled in View Attributes.

In your implementation, implement these methods.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        gestureStartPoint = [touch locationInView:self.view];
  }

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];    

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);


  if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
      //do something 
 }
else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){
      //do something
   }
 }

This is one way to implement a swipe recognizer. Also, you really should check out the Docs on this topic:

UISwipeGestureRecognizer

趁微风不噪 2024-10-13 22:55:04

UIGestureRecognizer 就是您想要的。特别是 UISwipeGestureRecognizer 子类

The UIGestureRecognizer is what you want. Especially the UISwipeGestureRecognizer subclass

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文