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?
change this
UIView *view = sender.view;
to this
UIButton *btn = (UIButton*)sender.view;
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With