开发者

Multiple UIGestureRecognizer and UIScrollView causes conflicts

Im developing a game which you could compare to a jigsaw puzzle where i have several game tiles that are going to be matched with each other on a surface (later referenced asMainView). At the beginning of the game the tiles will be placed under the MainView in a IdleView. I move the tiles with the pan gesture. I select a tile by using the tap gesture and finally I need to be able to rotate the tiles I do this with the rotate gesture. This caused some problems which I managed to solve but I then had to place the tiles in a UIScrollView since I have to many to be able to display them all at once.

I have done a couple of different implementations and they all caused some problems. Ill go through the different approaches I have tried.

Approach 1. Pan, Tap -> Tile, Rotate -> MainView

First I put the tap and pan recognizers in the game tile and the rotate gesture in my main view because the tiles are to small to perform a rotate gesture in. I then got a conflict when i accidently pressed another tile when I did the rotate gesture and then that tile interpreted it as a pan gesture. This lead to that I tried a new approach.

Approach 2. Pan, Tap, Rotate -> MainView

My second approach was to put all gesture recognizers in the MainView. Then each time a gesture is recognized I check if there is a tile at the location for the tap and pan gestures by calling CGRectContainsPoint([gameTile frame], gesturePoint);

Then fore the rotation gesture I check if a tile is selected then call the corresponding methods. This worked great! But then I realised that I had to have some mechanism to allow me to have more tiles then can be shown on an entire screen. So I added a UIScrollView where I placed the tiles horizontally so all can be accessed. I have made a little drawing below.

---------------------
|                   |
|                   |
|                   |
|    Height 370 p   |
|                   |
|      MainView     |
|                   |
|        开发者_开发问答           |
|                   |
|                   |
|                   |
|                   |
---------------------
|     Height 110 p  |
|      IdleView     |
|     UIScrollView  |
|                   |
|                   |
---------------------

I then added the GameTiles as subviews to the scrollview so that they would scroll with the view. Then they no longer listened to my touches since they weren't subviews of my MainView. So now I'm in somewhat of a stalemate where I no longer know what to do. I can move the tiles if I go back to approach 1 and set canCancelContentTouches = NO for the UIScrollView. But then I will have problems with the rotate gesture again and have to do some collision detection so that I can move the tile from the scrollview to the mainview.

I have read a lot of differents posts about this issue but I haven't been able to make sense of it yet.

Can anyone give me some pointers on either another way then a UIScrollView that allows me to have like 50 tiles that doesn't mess with my touches and still allow them to have a reasonable size so I can use gesture recognizers on them. Or how to smoothly move a tile from the UIScrollView on to the MainView and still be able to rotate the tiles without panning other tiles.


I don't have an exact answer to your question, but these tips might help:

  1. the gesture events always flow to the most deeply nested view

  2. you can subclass a GestureRecognizer and override the method "ignoreTouch:forEvent:" to conditionally ignore events and resolve conflicts. (as in your first attempt - you might want to ignore taps while rotation is going on)

I recently learned this stuff by watching the Apple WWDC videos referenced in this answer.

The talk features multiple objects that can be moved, scaled and rotated.

Apple example code for same

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜