简体   繁体   English

如何检测滑动方向?

[英]How to detect swipe direction?

I would like to detect when a user makes a swipe gesture, and detect it's direction (left, right, up or down). 我想检测用户何时做出滑动手势,并检测其方向(左,右,上或下)。 I need to detect it not when the gesture is finished, but just when the iPhone knows the direction, even if it's not finished. 我不需要检测手势是否结束,而是当iPhone知道方向时就检测它,即使它还没有完成。

I am using XCode 5, and designing for iPhone 5 with iOS 7. 我正在使用XCode 5,并为具有iOS 7的iPhone 5设计。

I would like to know the code that I have to paste to the .h, and to the .m, and if I have to drag and drop any item to the mainView. 我想知道我必须粘贴到.h和.m的代码,以及是否必须将任何项目拖放到mainView的代码。

If you want to know the direction when the gesture is not finished you would probably need the UIPanGestureRecognizer and detect the direction using the velocityInView: method. 如果您想知道手势未完成时的方向,则可能需要UIPanGestureRecognizer并使用velocityInView:方法检测方向。

in the *.h file: 在* .h文件中:

@interface ViewController : UIViewController

@property (nonatomic, strong) UIPanGestureRecognizer *recognizer;

@end

in the *.m file: 在* .m文件中:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];

    [self.view addGestureRecognizer:self.recognizer];
}

-(void)panGesture:(UIPanGestureRecognizer *)sender
{
    NSLog(@"Velocity: %@", NSStringFromCGPoint([sender velocityInView:self.view]));

    // Here You need to determine the direction
}
@end

You can easily get using UISwipeGestureRecognizers property directions... After :---- 您可以轻松地使用UISwipeGestureRecognizers属性指示...之后:----

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.yourView addGestureRecognizer:swipeRight];
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.gifWebView addGestureRecognizer:swipeLeft];

-(void)swipeRight{
//Do whatever you want
}
-(void)swipeLeft{
//Do whatever you want
}

And For Before Swipe you can use UIPanGestureRecognizer 对于“刷卡前”,您可以使用UIPanGestureRecognizer

I hope it help you 希望对您有帮助

From Docs : 文档

Gestures are either discrete or continuous. 手势既可以是离散的也可以是连续的。 A discrete gesture, such as a tap, occurs once. 离散手势(例如轻击)发生一次。 A continuous gesture, such as pinching, takes place over a period of time. 连续的手势(例如捏合)会在一段时间内发生。 For discrete gestures, a gesture recognizer sends its target a single action message. 对于离散手势,手势识别器向其目标发送单个动作消息。 A gesture recognizer for continuous gestures keeps sending action messages to its target until the multitouch sequence ends, 连续手势的手势识别器会不断向其目标发送动作消息,直到多点触摸序列结束为止,

So if you need to "detect it not when the gesture is finished, but just when the iphone knows the direction, even if it's not finished" you could not use UISwipeGestureRecognizer since it is a discrete one. 因此,如果您需要“不是在手势完成时就检测到它,而是在iPhone知道方向时就检测到它,即使它还没有完成”,则您不能使用UISwipeGestureRecognizer,因为它是离散的。 You should use UIPanGestureRecognizer and analyse it is results to check is there a swipe gesture or not. 您应该使用UIPanGestureRecognizer并分析其结果以检查是否有滑动手势。

To have a good example how it works, you can download a sample code provided by Apple: 要获得一个很好的例子,您可以下载Apple提供的示例代码:

https://developer.apple.com/library/ios/samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460 https://developer.apple.com/library/ios/samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460

Hope that helps ;) 希望能有所帮助;)

Swipe gestures are very quick. 滑动手势非常快。 Swipe gesture recognizers are designed to fire once, not continuously. 滑动手势识别器旨在触发一次,而不是连续触发。 I don't think you will be able to get information about a swipe gesture that is "in flight" very easily. 我认为您无法轻松获得有关“飞行中”轻扫手势的信息。

If this is urgent, you will probably have to create a custom subclass of swipe gesture recognizer and add additional logic and delegate messages that would notify the delegate once the gesture recognizer decides that it has detected a swipe. 如果很紧急,您可能必须创建一个轻扫手势识别器的自定义子类,并添加其他逻辑和委托消息,一旦手势识别器确定已检测到轻扫,它们就会通知委托。 That is way beyond your current skill level however. 但是,那超出了您当前的技能水平。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM