当我UIImageView
在几个UIButton
子类实例后面添加一个子类(全屏)时,这些按钮停止接收触摸并且不再起作用。我尝试过的任何方法都无法恢复它们与用户触摸的连接。
我已经尝试过:
a)使用UIImageView
前视图的子类(接收触摸)并确保setUserInteractionEnabled
是。结果:UIButtons
存在时没有响应UIView
。
b) 明确地将触摸从前视图传递给视图控制器,希望它会做正确的事情。结果:UIButtons
对触摸没有反应。
c)当收到正面的任何触摸时,直接在我想与之交互的按钮上调用touchesBegan
,touchesMoved
和方法。结果:引人注目的是,即使我将触摸事件塞进它们的喉咙,按钮也没有响应。touchesEnded
UIView
d) 将触摸从前视图传递到UIViewController
,并遍历所有子视图手动分发触摸。我提供了一个touchesBegan
方法来展示我是如何尝试做到这一点的。执行时循环是无限的,它永远不会超过第一个视图(类型为FrameUIView
)。
我知道这个问题很可能是我对正确的视图层次结构或响应者链缺乏了解的结果。
如果我评论按钮后创建的最后一个视图,则按钮可以正常工作。在这方面有什么特别之处UIButtons
吗?
这是代码。
我有一个 UIViewController 子类,其中:
- (void)loadView {
mainAppView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.view = mainAppView;
frameImageView = [[FrameUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
frameImageView.image = [UIImage imageNamed:[[[ThemeController sharedInstance] currentTheme] frameImageFilename]];
[frameImageView setUserInteractionEnabled:YES];
[mainAppView addSubview:frameImageView];
zoomInButton = [[CustomUIButton alloc] initWithFrame:CGRectMake(controlsOriginX + zoomWidth + lightWidth, controlsOriginY, zoomWidth, controlsHeight)];
[zoomInButton setTitle:@"+" forState:UIControlStateNormal];
[[zoomInButton titleLabel] setFont:[UIFont systemFontOfSize:28]];
[zoomInButton addTarget:self action:@selector(doMyAction) forControlEvents:UIControlEventTouchDown];
[zoomInButton addTarget:self action:@selector(cancelMyAction) forControlEvents:UIControlEventTouchUpInside];
[mainAppView addSubview:zoomInButton];
FrameFrontUIView *frameFrontImageView = [[FrameFrontUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
frameFrontImageView.image = [UIImage imageNamed:[[[ThemeController sharedInstance] currentTheme] frameFrontImageFilename]];
[frameFrontImageView setUserInteractionEnabled:YES];
[mainAppView addSubview:frameFrontImageView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView *view in self.view.subviews) {
NSLog(@"view is a %@", [view description]);
[view touchesBegan:touches withEvent:event];
}
}
任何知道如何做到这一点的人都会赢得互联网和 cookie。还接受俄罗斯轮盘赌混乱的左轮手枪。谢谢你。