简体   繁体   English

子类化以检测其背后的对象时,在透明UIView上使用时,手势不起作用

[英]Gesture is not working when used on transparent UIView when subclassed to detect objects behind it

在此处输入图片说明

Hi all, 大家好,

I am having some issue with detecting touches. 我在检测触摸时遇到一些问题。 Please refer image where yellow and brown are my UIView which is subclassed to detect the transparent touches. 请参考其中黄色和棕色是我的UIView的图像,该图像被子类化以检测透明的触摸。 I have added three gesture recognizers pan, tap and rotation on that UIViews, but when I subclass UIView and override the - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event method to detect all the Imageviews added, gesture recognizers stop working outside the ImageView. 我在该UIViews上添加了三个手势识别器平移,点击和旋转,但是当我将UIView子类化并使用-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event方法来检测所有添加的Imageviews时,手势识别器停止在ImageView之外工作。 And if I am not using subclass then it's unable to detect touches for ImageView on the yellow UIView. 而且,如果我不使用子类,则无法检测黄色UIView上ImageView的触摸。 I tried the solution from this link 我尝试过此链接的解决方案

Forwarding UIGesture to views behind 转发UIGesture到后面的视图

though it is not the same as my requirement, but it didn't help! 尽管它与我的要求不同,但是并没有帮助! Any help/hint is appreciated! 任何帮助/提示表示赞赏! Thanks in advance. 提前致谢。

I have resolved the issue myself. 我已经解决了这个问题。 I achieved the my target by applying gesture recognizers on superview not separate for each uiimageview or the colored uiviews. 我通过在手势视图上应用手势识别器(不是针对每个uiimageview或彩色uiview单独设置)实现了我的目标。 I have added uiviews on superview and now gesture is getting applied to imageview also. 我在超级视图上添加了uiviews,现在手势也被应用到了imageview。 And also changed the code in gesture actions so that gesture will get applied to touched imageview and not on superview. 并且还更改了手势操作中的代码,以便将手势应用于触摸的图像视图,而不应用于超级视图。

Try implementing gestureRecognizerShould begin in your subclass, test to see if the class of the recognizer is those classes [gestureRecognizer class] == [UITapGestureRecognizer class] for example, and return yes if it is. 尝试实现gestureRecognizer,应从您的子类开始,测试以查看识别器的类是否为那些类,例如[gestureRecognizer类] == [UITapGestureRecognizer类],如果是,则返回yes。

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/gestureRecognizerShouldBegin : http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/gestureRecognizerShouldBegin

The only way I've found to fix this is to add your stack of views as children to a UIView (A). 我发现解决此问题的唯一方法是将您的视图堆栈作为子级添加到UIView(A)。 It should be the same size as your transparent view/ all of the child views on your stack. 它的大小应与透明视图/堆栈中所有子视图的大小相同。 You should implement the gesture recogniser in the view controller if and connect (A) as the iboutlet (just drag and drop a gesture recogniser onto A in interface builder for example). 如果并且将(A)作为iboutlet连接,则应该在视图控制器中实现手势识别器(例如,只需将手势识别器拖放到接口构建器中的A上)。

(View Controller)
 |->(A)
     |->(1) Need gesture 
     |->(2) Need another gesture
     |->(3) Transparent (messing everything up)

Connect the gesture handlers to the appropriate children of A who need to handle them. 将手势处理程序连接到需要处理它们的A的适当子级。

For View Controller 对于View Controller

@interface AUIViewController : UIViewController <bla, bla, UIGestureRecognizerDelegate >

For (1) 对于(1)

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{/*yada yada*/}

In my opinion, this is an apple bug. 我认为这是一个苹果虫。

My answer is pretty much this but it is a work around for what is in my opinion, a bug. 我的答案差不多是这个,但是我认为它是一个错误,可以解决。

PS, sorry I didn't fix this without using interface builder. PS,对不起,我没有使用界面生成器无法解决此问题。 Hope its still helpful 希望它仍然有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM