检测用户是否未触摸对象

时间:2023-01-22 21:16:49

I'm developing an iOS app for iPad. I use I code to detect when does the user touch an object, but now I'd like to use the same code to detect when the user does not touch the object. This is the code:

我正在为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];

}
}

So how can I detect when the user touches the screen but not a certain object?

那么如何检测用户何时触摸屏幕而不是某个对象呢?

1 个解决方案

#1


3  

Actually CGRectContainsPoint will return false if the touch point is not on the certain object. Say you want to check if the touch point is in ribbon or not. Only a "!" will be enough.

实际上,如果触摸点不在某个对象上,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];
    }
}

#1


3  

Actually CGRectContainsPoint will return false if the touch point is not on the certain object. Say you want to check if the touch point is in ribbon or not. Only a "!" will be enough.

实际上,如果触摸点不在某个对象上,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];
    }
}