自定义iOS手势 [英] custom iOS gesture

查看:103
本文介绍了自定义iOS手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to iOS/objective-C and I wanted to know how to build custom gestures. In particular, if a user taps the top right of the screen and slides his/her finger down the edge of the device to the bottom (same gesture for the left hand side). I read through this:

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

但我想我无法弄清楚如何将它应用到我的身上特定情况。

But I guess I am having trouble figuring out how to apply it to my specific case.

推荐答案

创建 UIGestureRecognizer 子类有点涉及到以坚实的方式做。我非常推荐观看有关主题的WWDC2010视频会话120 - 使用手势识别器简化触摸事件处理& 第121节 - 高级手势识别。它们彻底而且做得很好。

Creating a UIGestureRecognizer subclass is a bit involved to do in a solid way. I very much recommend watching the WWDC2010 videos on the subject Session 120 - Simplifying Touch Event Handling with Gesture Recognizers & Session 121 - Advanced Gesture Recognition. They are thorough and well done.

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

But for a very simple example, based on your question, I created a very simple gesture recognizer that fires when a user touches the upper left quadrant of the attached view and slides their finger down to the lower right quadrant of the attached view and picks up their finger, without sliding to the left side of the attached view.

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

因此,手势识别器基本上可以获得标准触摸事件。 (他们不是,但他们这样做)。在您回复动作时,您可以更改手势识别器的属性。

So basically the gesture recognizer gets what seems like the standard touch events. (They're not, but they act that way). As you respond to the movements you change the state property of your gesture recognizer.

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

There are two basic types of recognizers, "Discrete" (think tap gesture) and "Continuous" (think pan gesture). Both types automatically start in UIGestureRecognizerStatePossible in the beginning.

对于离散类型,如此类型,目标是尽快到达州$ code> UIGestureRecognizerStateRecognized 或 UIGestureRecognizerStateFailed

For a "Discrete" type, like this one, your goal is to get to state UIGestureRecognizerStateRecognized or UIGestureRecognizerStateFailed as soon as possible.

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

The ideal usage of this example would be to add the RightSlidedown gesture recognizer to the main view of a new "Single View Application" in the view controller's viewDidLoad.

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

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

Then a simple action method is all that's required, like so:

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

这篇关于自定义iOS手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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