我已经像这样在按钮点击时以编程方式创建了一个 UIView
self.paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height+100)];
[self.paintView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
[self.navigationController.view addSubview:self.paintView];
并将其添加到 navigationController
以便我可以全屏查看我的 UIView。
但是当我尝试检测触摸时它不起作用
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view==self.paintView && self.paintView ) {
[UIView animateWithDuration:0.9 animations:^{
[self.sideView setFrame:CGRectMake(-290, 0, self.sideView.frame.size.width, self.sideView.frame.size.height)];
}];
}
}
此方法现在根本无法检测。
最佳答案
你能试试下面的代码来添加 View 和检查吗:
self.paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height+100)];
[self.paintView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
[self.window.rootViewController.view addSubview:self.paintView];//try this
由通用 UIViewController
创建的 View 填充窗口并位于您的 SubView
前面。因此,您的 SubView
无法接收触摸。
rootViewController.view
成为您窗口中的单个顶级 View ,并在其下添加 subview 并检查。
关于ios - TouchsBegan 无法在 iOS 中的 UIView 上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40055547/