2013-04-30 40 views
0

我正在開發iPad的iOS應用程序。我使用I代碼來檢測用戶何時觸摸對象,但現在我想使用相同的代碼來檢測用戶何時不觸摸對象。這是代碼:檢測用戶是否未觸摸對象

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
UITouch *touch = [touches anyObject]; 
CGPoint location = [touch locationInView:[touch view]]; 

if (CGRectContainsPoint(ribbon.frame, location) || CGRectContainsPoint(inferior.frame, location) || CGRectContainsPoint(superior.frame, location) & (pujat==YES)){ 
    pujat=NO; 
    [UIView animateWithDuration:0.25 animations:^{ 
     superior.frame = CGRectMake(0, 710, 1024,500); 
     ribbon.frame = CGRectMake(480, 685, 70,70); 
     inferior.frame = CGRectMake(0, 750, 1024,500);}]; 

    [self.view bringSubviewToFront:inferior]; 

} 
} 

所以,我怎麼能當用戶觸摸屏幕,但沒有一定的物體檢測?

+1

'else'子句? – CodaFi 2013-04-30 22:05:36

回答

3

實際上,如果觸點不在某個對象上,CGRectContainsPoint將返回false。假設您想檢查觸點是否在功能區中。只有一個「!」就足夠了。

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:[touch view]]; 

    if (CGRectContainsPoint(ribbon.frame, location) || CGRectContainsPoint(inferior.frame,  location) || CGRectContainsPoint(superior.frame, location) & (pujat==YES)){ 

     if(!CGRectContainsPoints(ribbon.frame,location)) 
      NSLog("Touch point is not on ribbon"); 

     pujat=NO; 
     [UIView animateWithDuration:0.25 animations:^{ 
     superior.frame = CGRectMake(0, 710, 1024,500); 
     ribbon.frame = CGRectMake(480, 685, 70,70); 
     inferior.frame = CGRectMake(0, 750, 1024,500);}]; 
     [self.view bringSubviewToFront:inferior]; 
    } 
}