简体   繁体   English

如何初始化UIGestureRecognizer?

[英]How do you initialize a UIGestureRecognizer?

I want to add a gesture recognizer to my button so that I can run code if the user swiped past the buttons frame. 我想向我的按钮添加一个手势识别器,以便在用户滑过按钮框架时可以运行代码。 I also want this code to be different if the swipe was up, right, left, or down the button. 如果向上,向右,向左或向下滑动按钮,我也希望此代码有所不同。

-(void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame=CGRectMake(0, 0, 100, 100);
    [self.view addSubview:button];
    UIGestureRecognizer *swipe=[[UIGestureRecognizer alloc]initWithTarget:button action:@selector(detectSwipe)];
    [button addGestureRecognizer:swipe];
}

so, did I do the initWithTarget:action: thing correct? 所以,我是否做了initWithTarget:action:正确的事情? And now that I do this how do i Implement the detectSwipe method? 现在,我这样做了,我该如何实现detectSwipe方法?

here is my idea on how to implement detectSwipe 这是我关于如何实现detectSwipe

          -(IBAction)detectSwipe:(UIButton *)sender
        {
      /* I dont know how to put this in code but i would need something like, 
if (the swipe direction is forward and the swipe is > sender.frame ){ 
[self ForwardSwipeMethod];
    } else if //same thing for right
    else if //same thing for left
    else if //same thing for down

        }

No, it isn't correct. 不,这是不正确的。 The target of the gesture recognizer is not the button, it's the object on which it calls the action method when detecting a gesture (otherwise how would it know on which object call that method? In OO, a method call/message send needs an explicit method name and an instance or class). 手势识别器的目标不是按钮,而是检测到手势时在其上调用动作方法的对象(否则它将如何知道在哪个对象上调用该方法?在OO中,方法调用/消息发送需要显式的方法名称实例或类)。

So you would most likely want 所以你很可能想要

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];

You also don't create an instance of UIGestureRecognizer directly but one if its concrete subclasses, UISwipeGestureRecognizer in this case. 您也不会直接创建UIGestureRecognizer的实例,而是直接创建一个实例(在本例中为UISwipeGestureRecognizer)。

After alloc-initting the recognizer, you attach it to the view you want to be recognized: 分配初始化识别器后,将其附加到要识别的视图上:

[button addGestureRecognizer:recognizer];

Then in the didSwipe: method, you can use the gesture recognizer's properties to decide what the size/distance/other property of the swipe was. 然后,在didSwipe:方法中,您可以使用手势识别器的属性来确定滑动的大小/距离/其他属性。

You better read some docs next time. 下次您最好阅读一些文档。

You got all right except for the target of gesture recogniser. 除了手势识别器的目标外,您一切都很好。 The target is an object that receives given selector message so your initWithTarget: call should accept self as an argument unless you're implementing detectSwipe method in a subclass of your button. 目标是接收给定选择器消息的对象,因此除非您在按钮的子类中实现detectSwipe方法,否则您的initWithTarget:调用应接受self作为参数。

You probably want to be using a UISwipeGestureRecognizer . 您可能想要使用UISwipeGestureRecognizer UIGestureRecognizer usually shouldnt be used unless you are subclassing it. 通常不应该使用UIGestureRecognizer,除非您将其子类化。 Your code should look similar to the following. 您的代码应类似于以下内容。

UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[button addGestureRecognizer:swipe];

H2CO3's answer is complete. H2CO3的答案是完整的。 Just don't forget that you're missing a colon ":" at the end of your selector! 只是不要忘记您在选择器的末尾缺少一个冒号“:”! It should be like this: @selector(detectSwipe:) 应该是这样的: @selector(detectSwipe:)

The colon ":" is because your method has an argument: (UIButton *)sender 冒号“:”是因为您的方法有一个参数: (UIButton *)sender

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

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