ios - TouchsBegan 无法在 iOS 中的 UIView 上工作?

标签 ios objective-c uiview uigesturerecognizer uitouch

我已经像这样在按钮点击时以编程方式创建了一个 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/

相关文章:

ios - UIViewController <UIKeyInput> 弹回时显示键盘

iphone - 新手指南 : Managing currency in iPhone app

iphone - 确定 UIView 对用户是否可见?

ios - bringSubviewToFront(_ :) function breaks a UIPanGestureRecognizer

ios - UIView 工厂和委托(delegate)

ios - 无论设备方向如何,物体都会落向地球

Objective-C 简单的 hello world 无法编译

c - Objective-C Singleton 实例作为静态?

ios - 从 UIView 中的 NSMutableArray 垂直显示 UILabel

ios - 调整 UIToolbar 大小会在 App Store 中被拒绝吗?