添加一个调用另一个类中的方法的手势

时间:2021-08-03 08:26:43

Simply I want to add a tap gesture for a UIImageView that when the user touches, calls a method that is implemented in another class (not the same class that contains the UIImageView).

简单地说,我想为UIImageView添加一个轻击手势,当用户触摸时,调用另一个类(不是包含UIImageView的同一个类)中实现的方法。

Can I do this and if so how can i do it ?

我可以这样做,如果是这样,我该怎么办?

4 个解决方案

#1


1  

You can do that. But you need the instance of the target class (class in which method is going to execute) as delegate or something in the gesture adding class.

你可以做到这一点。但是您需要将目标类的实例(将在其中执行方法的类)作为委托或手势添加类中的某些内容。

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self.delegate action:@selector(doSomething)];  

Here delegate will be the instance of the target class that you have to set before going to that class.

这里的委托将是你去那个类之前必须设置的目标类的实例。

Edit

I will explain a little more. Suppose you have two view controller classes VCA and VCB. you want to call a method in VCA from VCB through a tap gesture.

我会解释一下。假设您有两个视图控制器类VCA和VCB。你想通过点击手势从VCB调用VCA中的方法。

in VCB.h

@property (nonatomic,assign)VCA *delegate;

in VCB.m

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self.delegate action:@selector(doSomething)]; 

in VCA.m

You will present/ push VCB

您将呈现/推送VCB

 VCB * viewController = [[VCB alloc]init];
 viewController.delegate = self;
 // push or present viewController

#2


0  

Declare recognizer selector on init

在init上声明识别器选择器

UITapGestureRecognizer *recognizer = 
[UITapGestureRecognizer 
    initWithTarget:objectOfAnotherClass 
    action:@selector(methodImplementedOnObjectOfAnotherClass:)];
  • dont forget about defining number of taps (numberOfTapsRequired) and optional gesture recognizer required to fail (requireGestureRecognizerToFail:)
  • 不要忘记定义点击次数(numberOfTapsRequired)和失败所需的可选手势识别器(requireGestureRecognizerToFail :)

Link to article about selector.

链接到关于选择器的文章。

#3


0  

One straightforward approach is calling the method of another class from the selector method of tapGesture ie.

一种直接的方法是从tapGesture的选择器方法调用另一个类的方法即。

Add UITapGestureRecogniser to the imageview

将UITapGestureRecogniser添加到imageview

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapRecognised)];
[imageView addGestureRecognizer:tapGesture];

Dont forget to enable the userInteraction of the UIImageView, because by default for imageview userInteraction is disabled.

不要忘记启用UIImageView的userInteraction,因为默认情况下对于imageview userInteraction是禁用的。

you can do this like below.

你可以像下面这样做。

imageView.userInteractionEnabled = YES;

Then in the selector of tapGestureRecogniser call the method of another class

然后在tapGestureRecogniser的选择器中调用另一个类的方法

- (void)tapRecognised
{
    [next tappedOnImage];
}

Here next is the object of another class. You can use delegation also to call the method.

接下来是另一个类的对象。您也可以使用委托来调用该方法。

Hope this will help you.

希望这会帮助你。

#4


0  

here self.desired view is the view in which you want to add gesture and you should add gesture in following way

这里self.desired视图是你想添加手势的视图,你应该按照以下方式添加手势

and "webViewTapped.delegate = self" means you want to call a function of the same class when user tap and you can change it to your desired function by using delegate like self.delegate

和“webViewTapped.delegate = self”意味着你想在用户点击时调用同一个类的函数,你可以使用委托像self.delegate将它改成你想要的函数

UITapGestureRecognizer *viewTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        webViewTapped.numberOfTapsRequired = 1;
        webViewTapped.delegate = self;
        [self.desiredView addGestureRecognizer:viewTapped];



- (void)tapAction:(UITapGestureRecognizer *)sender;
{
// do your stuff here
}

#1


1  

You can do that. But you need the instance of the target class (class in which method is going to execute) as delegate or something in the gesture adding class.

你可以做到这一点。但是您需要将目标类的实例(将在其中执行方法的类)作为委托或手势添加类中的某些内容。

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self.delegate action:@selector(doSomething)];  

Here delegate will be the instance of the target class that you have to set before going to that class.

这里的委托将是你去那个类之前必须设置的目标类的实例。

Edit

I will explain a little more. Suppose you have two view controller classes VCA and VCB. you want to call a method in VCA from VCB through a tap gesture.

我会解释一下。假设您有两个视图控制器类VCA和VCB。你想通过点击手势从VCB调用VCA中的方法。

in VCB.h

@property (nonatomic,assign)VCA *delegate;

in VCB.m

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self.delegate action:@selector(doSomething)]; 

in VCA.m

You will present/ push VCB

您将呈现/推送VCB

 VCB * viewController = [[VCB alloc]init];
 viewController.delegate = self;
 // push or present viewController

#2


0  

Declare recognizer selector on init

在init上声明识别器选择器

UITapGestureRecognizer *recognizer = 
[UITapGestureRecognizer 
    initWithTarget:objectOfAnotherClass 
    action:@selector(methodImplementedOnObjectOfAnotherClass:)];
  • dont forget about defining number of taps (numberOfTapsRequired) and optional gesture recognizer required to fail (requireGestureRecognizerToFail:)
  • 不要忘记定义点击次数(numberOfTapsRequired)和失败所需的可选手势识别器(requireGestureRecognizerToFail :)

Link to article about selector.

链接到关于选择器的文章。

#3


0  

One straightforward approach is calling the method of another class from the selector method of tapGesture ie.

一种直接的方法是从tapGesture的选择器方法调用另一个类的方法即。

Add UITapGestureRecogniser to the imageview

将UITapGestureRecogniser添加到imageview

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapRecognised)];
[imageView addGestureRecognizer:tapGesture];

Dont forget to enable the userInteraction of the UIImageView, because by default for imageview userInteraction is disabled.

不要忘记启用UIImageView的userInteraction,因为默认情况下对于imageview userInteraction是禁用的。

you can do this like below.

你可以像下面这样做。

imageView.userInteractionEnabled = YES;

Then in the selector of tapGestureRecogniser call the method of another class

然后在tapGestureRecogniser的选择器中调用另一个类的方法

- (void)tapRecognised
{
    [next tappedOnImage];
}

Here next is the object of another class. You can use delegation also to call the method.

接下来是另一个类的对象。您也可以使用委托来调用该方法。

Hope this will help you.

希望这会帮助你。

#4


0  

here self.desired view is the view in which you want to add gesture and you should add gesture in following way

这里self.desired视图是你想添加手势的视图,你应该按照以下方式添加手势

and "webViewTapped.delegate = self" means you want to call a function of the same class when user tap and you can change it to your desired function by using delegate like self.delegate

和“webViewTapped.delegate = self”意味着你想在用户点击时调用同一个类的函数,你可以使用委托像self.delegate将它改成你想要的函数

UITapGestureRecognizer *viewTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        webViewTapped.numberOfTapsRequired = 1;
        webViewTapped.delegate = self;
        [self.desiredView addGestureRecognizer:viewTapped];



- (void)tapAction:(UITapGestureRecognizer *)sender;
{
// do your stuff here
}