Search code examples
iosobjective-cuiimageviewframetouches

Keep From Pushing 2 View Controllers at Once


In my app setup, I have a navigation controller with 4 ImageViews. 1 of them can be dragged around, while the other 3 are stationary at the top section of the view. Using the code below, I have it set up so that the user drags the one image view to the image view of where he wants to go. So to get to view 1, he drags the movable image view to image view 1, and so on. The issue is that with the width of the image views, it is possible for the selector view to touch two at one time, which creates a nesting view controller issue. Is there a way I can keep this from happening, short of moving the image views so far away that it is impossible for more than one to be selected at a time?

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    // If the touch was in the placardView, move the placardView to its location
    if ([touch view] == clock) {
        CGPoint location = [touch locationInView:self.tabBarController.view];
        clock.center = location;
        BOOL isIntersecting = CGRectIntersectsRect(clock.frame, prayer.frame);
        BOOL isIntersecting2 = CGRectIntersectsRect(clock.frame, fasting.frame);
        BOOL isIntersecting3 = CGRectIntersectsRect(clock.frame, study.frame);

        if(isIntersecting){
            [self schedulePrayer];
            NSLog(@"prayer");
        }
        if(isIntersecting2){
            [self scheduleFasting];
            NSLog(@"fasting");
        }
        if(isIntersecting3){
            [self scheduleStudying];
            NSLog(@"Studying");
        }
        return;
    }
}

Solution

  • Why don't you just use if ... else if ... else if?

     if(isIntersecting){
                [self schedulePrayer];
                NSLog(@"prayer");
            }
     else if(isIntersecting2){
                [self scheduleFasting];
                NSLog(@"fasting");
            }
     else if(isIntersecting3){
                [self scheduleStudying];
                NSLog(@"Studying");
            }
    

    Then, only one will be triggered at a time.