简体   繁体   中英

Disable user interaction on one UIView as the other UIView is swiped

Before any starts, I understand yourView.userInteractionEnabled = NO; is an option, but let me explain first the circumstance.

I have these 2 UIView objects, stoneOne and stoneTwo . I have 4 UISwipeGestureRecognizer objects attached to them for up, down, left and right. Imagine swiping these 'stones' around a 5x5 grid.

What I don't want is to be able to swipe both at the same time.

Currently, that bug is still an issue. I'll show you a method called swipeLeft: which represents the layout for all swipe directions.

    - (IBAction)swipeLeft:(UISwipeGestureRecognizer *)recognizer {
        _oldMove1 = _move1;
        _oldMove2 = _move2;
        if (recognizer.view == _oneStone
               && recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
            _twoStone.userInteractionEnabled = NO;
            _oneStone = recognizer.view;
            [self moveOne:CGPointMake(-1, 0) withView:_oneStone];
            self.move1++;
            // 'causeADelay:' runs _twoStone.userInteractionEnabled = YES;
            [self performSelector:@selector(causeADelay:) withObject:_twoStone afterDelay:1];
        } else if (recognizer.view == _twoStone
               && recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
            _oneStone.userInteractionEnabled = NO;
            _twoStone = recognizer.view;
            [self moveTwo:CGPointMake(-1, 0) withView:_twoStone];
            self.move2++;
            [self performSelector:@selector(causeADelay:) withObject:_oneStone afterDelay:1];
        }
        self.moveCount++;

}

One of the things I tried was creating a delay on when I can interact with my UIView objects. This worked ONLY IF I waited a split half-second to interact. The full delay would occur, and everything would work.

My bug is when you swipe them both at the same time. Is that because of the swipe gestures attached to them?

I also tried removing and reapplying the objects as subviews...didn't work, obviously. I really need this to work otherwise I have a dead-end game. I was very new to coding when I first started and never thought about Cocos2d or other game-driven platforms for development, so everything I have came from on-the-fly thinking.

There are several solutions, but here's a particularly easy one:

Remove the swipe gesture recognizers from the stones and attach them instead to the common superview of the stones. This solves the problem, because only one gesture recognizer on the same view will recognize at any one time.

Of course, you will now have to use hit-testing to find out which stone (if any) is being swiped. But that's an easy implementation detail, and is a small price to pay.

And of course another cool feature is that you now only need four gesture recognizers total!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM