自定义iOS手势

我是iOS / Objective-C的新手,我想知道如何构build自定义手势。 特别是,如果用户轻击屏幕的右上angular,并将他/她的手指沿着设备的边缘向下滑动(与左手侧相同的手势)。 我读过这个:

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

但是我想我很难搞清楚如何将其应用于我的具体情况。

创build一个UIGestureRecognizer子类有点涉及到坚实的方式。 我非常推荐观看有关此主题的WWDC2010videoSession 120 - Simplifying Touch Event Handling with Gesture RecognizersSession 121 - Advanced Gesture Recognition Session 120 - Simplifying Touch Event Handling with Gesture Recognizers 。 他们彻底,做得好。

但是对于一个非常简单的例子,基于你的问题,我创build了一个非常简单的手势识别器,当用户触摸附属视图的左上象限并将其手指滑动到附属视图的右下象限时触发并拾取他们的手指,而不会滑到附图的左侧。

RightSlidedown.h:

 #import <UIKit/UIGestureRecognizerSubclass.h> // This import is essential @interface RightSlidedown : UIGestureRecognizer @end 

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 

所以基本上,手势识别器就会得到一些标准的触摸事件。 (他们不是,但他们这样做)。 当您响应这些动作时,您将更改手势识别器的state属性。

有两种基本types的识别器,“离散”(思考点击手势)和“连续”(思考平移手势)。 这两种types自动从UIGestureRecognizerStatePossible开始。

对于一个“离散”types,就像这样,你的目标是尽快到状态UIGestureRecognizerStateRecognizedUIGestureRecognizerStateFailed

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

 [self.view addGestureRecognizer:[[RightSlidedown alloc] initWithTarget:self action:@selector(rightSlide:)]]; 

那么一个简单的操作方法就是所有必需的,如下所示:

 -(void)rightSlide:(RightSlidedown *)rsd{ NSLog(@"right slide"); } 

您可以通过在触摸的x轴和y轴上查看正面或负面三angular洲来实现。 例如,复选标记手势(√)将是负的三angular洲,其后是y的正值,而x总是负值,触摸结束的高度低于开始时的高度。 添加更多的手指,你添加更多的检查

伪代码:

 bool firstStroke, secondStroke, motion, override; while (touchdown){ if (yDelta < 0){firstStroke = TRUE;} if (firstStroke && yDelta > 0){secondStroke = TRUE;} if (xDelta < 0){motion = TRUE;} if (xDelta > 0 || (firstStroke && secondStroke && yDelta < 0)){override = TRUE;} } if (firstStroke && secondStroke && motion && start.y > end.y && !override){ return TRUE; }else{ return FALSE; } 

while命令意味着当触摸停止时,检查3件事情:

– 如果触摸已经下移

– 如果触摸已经移动了,它又上移了

– 如果触摸正在从右向左移动

第四个检查是检查触摸是否从左到右移动,或者在手势完成之后手势向下移动之后。

触摸完成后,再次检查手势是否移动正确,点是否在正确位置开始和结束,以及手势是否移动不正确(覆盖)。

希望有所帮助。