Reputation: 101
I'm using UIGestureRecognizer
for pan, rotate, pinch. But i'm applying to whole view. I need to remove gesture for button other than subviews. But when i use pan the button also affecting. How to restrict button move from self.view. I used below code for UIPanGestureRecognizer
.
UIPanGestureRecognizer *dbpan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(ondbPan:)];
[self.view addGestureRecognizer:dbpan];
[closeButton removeGestureRecognizer:dbpan];
Pan:
- (void)ondbPan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
CGPoint offset = [gesture translationInView:self.view];
CGPoint center = gesture.view.center;
center.x += offset.x;
center.y += offset.y;
gesture.view.center = center;
[gesture setTranslation:CGPointZero inView:self.view];
}
}
Upvotes: 1
Views: 1634
Reputation: 14296
On Swift:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if (touch.view!.isKindOfClass(UIButton)) {
return false
}
return true
}
Note: Make sure UIGestureRecognizerDelegate is added and tapGesture.delegate = self on viewDidLoad().
Upvotes: 1
Reputation: 49720
Try with bellow code that delegate of UIGestureRecognizer
return FALSE if that subview is kind of class UIButton Class. also set delegate dbpan.delegate = self;
while you setting and add UIPanGestureRecognizer
.
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
{
return FALSE;
}
else
{
return TRUE;
}
}
UPDATE:-
I dont Know why that not working at your end i test this creating one demo as well using this code:-
.h file
@interface myviewcontroller : UIViewController<UIGestureRecognizerDelegate>
and .m class
- (void)pan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
CGPoint location = [gesture locationInView:self.view];
[demoView setCenter:location];
}
}
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
{
return FALSE;
}
else
{
return TRUE;
}
}
- (void)viewDidLoad
{
UIPanGestureRecognizer *dbpan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(pan:)];
dbpan.delegate=self;
[self.view addGestureRecognizer:dbpan];
[super viewDidLoad];
}
-(IBAction)B1called
{
NSLog(@"This is called button 1");
}
-(IBAction)B2called
{
NSLog(@"This is called button 2");
}
And it's Output is
Upvotes: 5
Reputation: 3273
Try this...
while (closeButton.gestureRecognizers.count) {
[closeButton removeGestureRecognizer:[closeButton.gestureRecognizers objectAtIndex:0]];
}
Upvotes: 0