/避免添加在view上的手势使button的UIControlEventTouchUpInside不响应 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return ([[touch.view class] isSubclassOfClass:[UIButton class]]) ? NO : YES; } //添加手势 - (void)setTheGesture { UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; UIView *myView = [self.view viewWithTag:1]; [myView addGestureRecognizer:singleTap]; } //此方法来自于 <http://www.devdiv.com/forum.php?mod=viewthread&action=printable&tid=101367> /* So while working on version 2.0 of Today's News, we came across the need to have a UIButton as a subview of a view which handles tap gestures. So the expected behavior is that if we tap inside the button, it should fire the corresponding selector for the UIControlEventTouchUpInside event, but otherwise it should fire the selector for the tap gesture recognizer. Unfortunately, it doesn't work that way by default. */ /*I found two solutions on Stack Overflow: Solution 1: Is to set the cancelsTouchesInView property for UITapGestureRecognizer. While this works, what happens is that the selector for the tap gesture is fired along with the selector for UIControlEventTouchUpInside - not ideal. Solution 2: Is what I chose, because it lets you decide whether the touch should be ignored or not. So as the Gist below describes, if the view corresponding to the touch is a subview of UIButton, then ignore, otherwise don't ignore. */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return ([[touch.view class] isSubclassOfClass:[UIButton class]]) ? NO : YES; } /* 其实你响应的 有可能不是UIButton,但没事,你可以根据[touch.view class]的打印来判断,类似这样,一个UIBarButtonItem显示的却是UIToolbarTextButton */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([[NSString stringWithFormat:@"%@",[touch.view class]] isEqualToString:@"UIToolbarTextButton"]) { return NO; } else { return YES; } //return ([[touch.view class] isSubclassOfClass:UIToolbarTextButton]) ? NO : YES; }
UIView添加手势,UIbutton不响应点击事件
最新推荐文章于 2019-03-29 09:34:07 发布