Saturday, December 04, 2010

touchesShouldCancelInContentView is a life saver!

After a few days of horrible experiences with UIScrollView on iOS, I finally came across the solution from someone at Stack Overflow.

The problem
I have a UIScrollView with a grid of tiles inside it (it's a letter game kind of like Scrabble). When a user places a tile on the board, we want them to be able to pick up the tile again without scrolling the scroll view (and without waiting 150ms). For anyone who's worked with UIScrollView on iOS before, you'll immediately recognize that this is an incredibly difficult thing to do.

The solution
My board controller knows exactly when the user has picked up a piece, and signals it in the isPlaying flag. Armed with this, all I needed to do was put an innocuous little function in my custom UIScrollView subclass:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
 return !controller.isPlaying;
}
This tells the scroll view to stop messing around with anything if the user has picked up a piece. Magic!

No comments: