Adding a tap Gesture that calls a method in anothe

2019-07-20 01:41发布

问题:

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).

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

回答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.

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 * viewController = [[VCB alloc]init];
 viewController.delegate = self;
 // push or present viewController


回答2:

Declare recognizer selector on 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:)

Link to article about selector.



回答3:

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

Add UITapGestureRecogniser to the 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.

you can do this like below.

imageView.userInteractionEnabled = YES;

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

- (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:

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

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

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
}