I am trying to create an app where UIButtons can be draged and dropped when a UILongPressGestureRecognizer gesture is fired.
I have:
UIGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
And
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:self.view];
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
//NSLog(@"handleLongPress: StateBegan");
break;
case UIGestureRecognizerStateChanged:
if(location.y > 75.0 && location.x > 25 && location.x < 300)
button.frame = CGRectMake(location.x-25, location.y-15, 50, 30);
break;
case UIGestureRecognizerStateEnded:
//NSLog(@"handleLongPress: StateEnded");
break;
default:
break;
}
}
This works great with one button (namely the ivar button
). How can I send to the handleLongPress function the current button that is being pressed? In other words, I would like to do something like the following where I pass in sender
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer:(id)sender {
CGPoint location = [recognizer locationInView:self.view];
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
//NSLog(@"handleLongPress: StateBegan");
break;
case UIGestureRecognizerStateChanged:
if(location.y > 75.0 && location.x > 25 && location.x < 300)
sender.frame = CGRectMake(location.x-25, location.y-15, 50, 30);
break;
case UIGestureRecognizerStateEnded:
//NSLog(@"handleLongPress: StateEnded");
break;
default:
break;
}
}
Did you try recognizer.view
?
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