Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

ios

iphone

By the following code I attatched a button in a 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

}
}

by this code sender.view I am getting the attached view as UIView But I want the view as it was attached (UIButton), how do I get the UIView as UIButton?

like image 446
Reyjohn Avatar asked Oct 23 '13 07:10

Reyjohn


3 Answers

change this

UIView *view = sender.view;

to this

UIButton *btn = (UIButton*)sender.view;
like image 116
Patel Avatar answered Nov 04 '22 18:11

Patel


Like this:

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

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

like image 28
Sergey Kalinichenko Avatar answered Nov 04 '22 18:11

Sergey Kalinichenko


UIView* yourView = yourGestureRecogniser.view;

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

like image 37
n00bProgrammer Avatar answered Nov 04 '22 17:11

n00bProgrammer