自定义iOS手势

use*_*008 2 objective-c uigesturerecognizer ios

我是iOS/objective-C的新手,我想知道如何构建自定义手势.特别地,如果用户轻敲屏幕的右上方并将他/她的手指沿着设备的边缘向下滑动到底部(左手侧的相同手势).我读完了这个:

https://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html#//apple_ref/doc/uid/TP40009541-CH6-SW2

但我想我无法弄清楚如何将它应用到我的具体案例中.

NJo*_*nes 6

创建UIGestureRecognizer子类有点涉及以可靠的方式进行.我非常建议你观看关于这个主题的WWDC2010视频Session 120 - Simplifying Touch Event Handling with Gesture Recognizers&Session 121 - Advanced Gesture Recognition.他们彻底而且做得很好.

但是对于一个非常简单的例子,根据你的问题,我创建了一个非常简单的手势识别器,当用户触摸附加视图的左上象限并将其手指向下滑动到附加视图的右下象限时触发并触发他们的手指,没有滑动到附加视图的左侧.

RightSlidedown.h:

#import <UIKit/UIGestureRecognizerSubclass.h> // This import is essential
@interface RightSlidedown : UIGestureRecognizer
@end
Run Code Online (Sandbox Code Playgroud)

RightSlidedown.m

#import "RightSlidedown.h"
@implementation RightSlidedown
-(id)initWithTarget:(id)target action:(SEL)action{
    if ((self = [super initWithTarget:target action:action])){
        // so simple there's no setup
    }
    return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if ([touch locationInView:self.view].x < CGRectGetMidX(self.view.bounds)) self.state = UIGestureRecognizerStateFailed;
    else if ([touch locationInView:self.view].y > CGRectGetMidY(self.view.bounds)) self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if([touch locationInView:self.view].x < CGRectGetMidX(self.view.bounds)) self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if ([touch locationInView:self.view].x < CGRectGetMidX(self.view.bounds)) self.state = UIGestureRecognizerStateFailed;
    else if ([touch locationInView:self.view].y < CGRectGetMidY(self.view.bounds)) self.state = UIGestureRecognizerStateFailed;
    else {
            // setting the state to recognized fires the target/action pair of the recognizer
        self.state = UIGestureRecognizerStateRecognized; 
    }
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    self.state = UIGestureRecognizerStateCancelled;
}
-(void)reset{
    // so simple there's no reset
}
@end
Run Code Online (Sandbox Code Playgroud)

因此,手势识别器基本上可以获得标准触摸事件.(他们不是,但他们这样做).当您对动作做出响应时,您将更改state手势识别器的属性.

有两种基本类型的识别器,"离散"(想想轻拍手势)和"连续"(想想平移手势).两种类型都会UIGestureRecognizerStatePossible在开始时自动启动.

对于"离散"型,像这样的,你的目标就是进入状态UIGestureRecognizerStateRecognizedUIGestureRecognizerStateFailed尽快.

此示例的理想用法是将RightSlidedown手势识别器添加到视图控制器中新的"单视图应用程序"的主视图中viewDidLoad.

[self.view addGestureRecognizer:[[RightSlidedown alloc] initWithTarget:self action:@selector(rightSlide:)]];
Run Code Online (Sandbox Code Playgroud)

然后,只需要一个简单的动作方法,如下所示:

-(void)rightSlide:(RightSlidedown *)rsd{
    NSLog(@"right slide");
}
Run Code Online (Sandbox Code Playgroud)