touches开始不能在iOS的UIView上工作



我已经创建了一个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)];
     }];
    }
}

这个方法现在根本检测不到

你可以试试下面的代码来添加视图和检查:

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创建的视图填充窗口,位于SubView的前面。因此你的SubView不能接收触摸。

rootViewController.view是您的窗口中的单个顶级视图,并在其下添加子视图并检查

如果视图的所有父视图上都启用了'userInteractionEnabled' NOT,就会发生这种情况。如果任何父视图将这个属性设置为false,触摸事件将不会为它们或它们的任何子视图处理。

让userInteractionEnabled在所有父视图上启用。如果父视图将此属性设置为false,则不会处理子视图的触摸。

你也可以使用UITapGestureRecognizer。

给你的UIView UserInteractionEnable = YES

把你的-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { }代码换成我的,你得到点。

CGPoint point = [touch locationInView:touch.view];
 CGPoint pointOnScreen = [touch.view convertPoint:point toView:nil];
 NSLog(@"Point - %f, %f", pointOnScreen.x, pointOnScreen.y);
 NSLog(@"Touch");

最新更新