2016-10-15 26 views
0

我已經創建了一個UIView編程按鈕點擊這樣touchesBegan無法在iOS中使用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)]; 
    }]; 
    } 

} 

這種方法不是現在在所有的檢測。

+0

這是因爲您使用通過代碼創建的一些新視圖阻止父視圖,touches方法屬於父視圖,但由於子視圖無法檢測到它。 – Dasem

回答

0

如果在視圖的所有超級視圖上啓用了「userInteractionEnabled」NOT,則會發生這種情況。如果任何superview將此屬性設置爲false,則不會爲他們或他們的任何子視圖處理觸摸事件。檢查是否可以成爲問題。

0

,給你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"); 
0

使所有superviews上的userInteractionEnabled有效。如果超級視圖將此屬性設置爲false,則觸摸將不會被處理子視圖。

您也可以使用UITapGestureRecognizer。

1

你可以試試下面的代碼添加視圖和檢查:

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是您窗口中的單個頂層視圖,並在其下添加子視圖並進行檢查。

相關問題