How to get the original attached view in the gesture recognizer in iphone?

In the following code, I pressed a button in the gesture recognizer:

UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addLongpressGesture:)]; [longPress setDelegate:self]; [BUTTON addGestureRecognizer:longPress]; 

Here is my addLongpressGesture method:

 - (void)addLongpressGesture:(UILongPressGestureRecognizer *)sender { UIView *view = sender.view; CGPoint point = [sender locationInView:view.superview]; if (sender.state == UIGestureRecognizerStateBegan){ // GESTURE STATE BEGAN } } 

using this sender.view code I get the attached view as a UIView But I want the view to be attached (UIButton), how do I get the UIView as UIButton?

+6
source share
3 answers

change this value

 UIView *view = sender.view; 

to that

 UIButton *btn = (UIButton*)sender.view; 
+12
source

Like this:

 UIButton *button = (UIButton*)sender.view; 

UIButton is a UIView . If you know that your gesture recognizer is attached to a button, this casting is safe.

+5
source
 UIView* yourView = yourGestureRecogniser.view; 

Since each gestureRecogniser has only one view property, this explains why the gesture recognizer can only be added to 1 view.

+2
source

Source: https://habr.com/ru/post/956535/


All Articles