Eric D'Souza
Eric D'Souza

Reputation: 690

animateWithDuration causing problems with touch events on ios4 after upgrade to xcode 4.6

I have an app that was running fine until I upgraded to xcode 4.6. After the upgrading the xcode 4.6, no touch events are received when running on ios4 (works fine on ios5 and ios6). I tried just adding a switch in IB (not hooked up, just added). I can toggle the switch in the simulator on ios5 and ios6 devices, but on ios4 I can't even move the switch.

I have a little animation that runs on a timer:

- (void)setFoamSize:(float)foamSize
{    
    // foamSize is a number between 0 - 100.  0 is min foam size; 100 is max foam size

    // NSLog(@"setFoamSize %f", foamSize);

    float centerX, centerY;
    float minHeight, minWidth;
    float maxHeight, maxWidth;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        centerX     = 70.0;
        centerY     = 102.0f;

        minWidth    = 51.0f;
        minHeight   = 37.0f;

        maxWidth    = 111.0f;
        maxHeight   = 86.0f;
    }
    else
    {
        centerX     = 158.0f;
        centerY     = 151.0f;

        minWidth    = 74.0f;
        minHeight   = 64.0f;

        maxWidth    = 320.0f;
        maxHeight   = 271.0f;
    }


    float height = ((maxHeight - minHeight) * foamSize + minHeight);
    float width  = ((maxWidth  - minWidth)  * foamSize + minWidth);

    float x = centerX - width / 2;
    float y = centerY - height / 2;

    float alpha  = MIN(1.0, ((1.0 - 0.7) * foamSize + 0.7));

    @autoreleasepool {

        [UIView animateWithDuration:1.0
                              delay:0.0
                            options: UIViewAnimationOptionCurveLinear
                         animations:^{
                             imageViewFoam.frame = CGRectMake(x, y, width, height);
                             imageViewFoam.alpha = alpha;
                         }
                         completion:^(BOOL finished){
                         }];
    }

}

If I comment out this animation, then the touch events are received in ios 4. The animation works in all iOS's, and takes up a small portion of the screen (ie not overlapping any of the buttons or switches).

Why would this animation interfering with touch events?

Update:

If I move the image changes outside of the animateWithDuration, everything works (ie, touch events are received in ios4)

imageViewFoam.frame = CGRectMake(x, y, width, height);
imageViewFoam.alpha = alpha;


@autoreleasepool {

    [UIView animateWithDuration:1.0
                          delay:0.0
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                        //imageViewFoam.frame = CGRectMake(x, y, width, height);
                        //imageViewFoam.alpha = alpha;
                     }
                     completion:^(BOOL finished){
                     }];
}

Update 2

The animation is kicked off every 1 second (so with the duration also at 1 second, I get a relatively smooth growth of the image. It's slightly jagging, but that's the effect I want).

In any case, if I change the duration to 0.5 seconds, then touch events work if the object is touched in between the animation (but not if they're touched during the animation).

So, it seems that while the animationWithDuration is actually animating something, that touch events are ignored in iOS4.

Upvotes: 1

Views: 1301

Answers (1)

Eric D'Souza
Eric D'Souza

Reputation: 690

I added the option UIViewAnimationOptionAllowUserInteraction

@autoreleasepool {

    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
                     animations:^{
                        imageViewFoam.frame = CGRectMake(x, y, width, height);
                        imageViewFoam.alpha = alpha;
                     }
                     completion:^(BOOL finished){
                     }];
}

Thanks to mmc's answer here

According to Apple docs the animation should only disable the user interaction for imageViewFoam

During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property. You can disable this behavior by specifying the UIViewAnimationOptionAllowUserInteraction option when configuring the animation.

So I'm not sure why things weren't working. In any case, adding the option solved my problem.

Upvotes: 3

Related Questions