开发者

UIView animations interfere with touch events

My app consists of a map-like interface and should do nothing else than showcasing an automatic route.

I have a painted (via picture) route, and the app is supposed to have a "currentPosition" marker to move automatic along the route, indicating the travel progress.

The animation is working well, but as soon as the user touches the screen (by accident etc..) the animations are completely misplaced, in wrong speed and can end up flickering like crazy.

First, 2 code samples:

    [UIView animateWithDuration:POINT_MOVEMENT_DURATION
                          delay:POINT_MOVEMENT_DELAY
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.currentPosition.center = mp.point;
                     }
                     completion:^(BOOL finished){
                         NSLog(@"finished animation: %i", finished); 

                         [self nextPoint:nil];
                     }
     ];

nextPoint is the function to go to the next point. mp is a variable holding the pixel coordinates on screen.

Now, when running this code, the animatin will go smoothly, as visible in the logs:

2011-01-02 15:32:11.006 MAP Player[2258:307] finished animation: 1
2011-01-02 15:32:14.008 MAP Player[2258:307] finished animation: 1
2011-01-02 15:32:17.011 MAP Player[2258:307] finished animation: 1

As soon as I touch the screen this will switch to 0.

2011-01-02 15:32:26.642 MAP Player[2258:307] finished animation: 0
2011-01-02 15:32:26.647 MAP Player[2258:307] finished animation: 0
2011-01-02 15:32:26.653 MAP Player[2258:307] finished animation: 0

And at this开发者_运维问答 moment, the item will flicker / move fast, wrong direction etc.

I found one thing: In the ViewController, once I set [self.view setUserInteractionEnabled:NO]; the flickering cannot happen again, so it definitely has something to do with the user interaction. It's also independent of SingleTouch / MultiTouch.

How can I create these animations touch-independent? I was thinking to add the setUserInteractionEnabled call into the animation responders. But this doesn't seem right?


You can just hold a variable, something like 'animationStarted', and doesn't run animation while another is in progress.


setUserInteractionEnabled is indeed the right example. Probably related to that is the MoveMe example by Apple. They are also using the setUserInteractionDisabled call. I think the key issue is to clearly understand where your view is at, what animations are running how and what will happen when they finish. And it's useful to know the proper functions to cancel / stop animations in a way that they won't generate any more events.

Additionally for my example, I had to add a Back button to the interface, meaning that I can also escape the controller somehow. Yes, it's not a simple standard UINavigationController because of custom UI.

So, to make the button still work, I had to push it UP to the self.view's superview:

[self.backButton removeFromSuperview];
[self.view.superview addSubview:self.backButton];

And now I can enjoy a nice animation & no interference. But hold on, when the animation is actually running, I still have to disable the user interaction for the backbutton. This is simply because once the animation finishes, it tries to access a member, that might have been dealloc'd or removed by that time. I will probaly try to call this next time:

[self.view.layer removeAllAnimations];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜