2013-04-30 50 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]; 
    } 
}