点击子视图时,UIView上的UITapGestureRecognizer及其子视图一起响应 [英] UITapGestureRecognizer on UIView and Its Subview Respond Together When Subview Is Tapped
问题描述
UITapGestureRecognizer
同时应用于UIImageView
及其子视图(UITextView
).但是,当我点击子视图时,接收器变为子视图及其父视图(即UIImageView
+ UITextView
).但是,它应该只是 subview ,因为那是我点击的那个.我以为嵌套手势会首先做出反应,但显然父母会先握住拳头,然后再交给孩子.
UITapGestureRecognizer
is applied to both UIImageView
and its subview (UITextView
). However, when I tap on subview, the receiver becomes subview and its parent view (i.e. UIImageView
+ UITextView
). It should however be only subview because that was the one I tapped. I was assuming nested gestures would react first but apparently parent receives the fist tap and then it goes to child.
因此,针对各种情况存在不同的解决方案(与我的不相似,但滚动视图内的按钮冲突).如何轻松地解决我的问题而无需可能的子类化以及对iOS 6+的支持?我尝试延迟UIImageView
上UIGestureRecognizer
的启动时触摸,并且尝试将cancelsTouchesInView
设置为NO
-都没有运气.
So, there are different solutions out there for various scenarios (not similar to mine but rather buttons inside scroll view conflict). How can I easily fix my issue without possible subclassing and for iOS 6+ support? I tried delaying touch on start for UIGestureRecognizer
on UIImageView
and I tried setting cancelsTouchesInView
to NO
- all with no luck.
推荐答案
尝试以下代码:
使<
conform the <UIGestureRecognizerDelegate
> to your class.
设置yourGesture.delegate = self
;
然后添加此委托方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// return YES (the default) to allow the gesture recognizer to examine the touch object, NO to prevent the gesture recognizer from seeing this touch object.
if([touch.view isKindOfClass: [UITextView class]] == YES)] {
return YES;
}
else {
return NO;
}
}
希望它能解决您的问题.享受编码.. !!!!
Hope it will solve your issue. Enjoy Coding..!!!!
这篇关于点击子视图时,UIView上的UITapGestureRecognizer及其子视图一起响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!