Search code examples
iosobjective-ccore-animationnstimer

animateWithDuration isn't animating UIButton


I have a category on UIView that should animate movement of a UIButton from one point to another. It is being run, but the button doesn't move at all. It seems like the animation is successful, but there's no movement at all.

The button is called by a repeating NSTimer, which counts down to a point where the button is animated (check every second to see if something is true, if it is remaining points--, when remaining points == 0 animate). There is also a timer being called every 0.01 seconds to do some work with levels on the microphone, but if I increase the frequency it still doesn't work.

I think my animation code is ok - but something is holding up the animation from displaying correctly. If I in viewDidLoad before animating it animates (although it starts at the end point and animates to where I've placed the button in the storyboard), but if I animate in the flow of the program it doesn't work.

The category correctly outpoints my end points, and after 5 seconds "done".

The Category:

@implementation UIView (Animation)
- (void) moveTo:(CGPoint)destination duration:(float)secs option:(UIViewAnimationOptions)option
{
    NSLog(@"moveTo Category. destination: %@ and duration %f", NSStringFromCGPoint(destination), secs);
    [UIView animateWithDuration:secs delay:0.0 options:option
                     animations:^{
                         self.frame = CGRectMake(destination.x,destination.y, self.frame.size.width, self.frame.size.height);
                         NSLog(@"self.frame: %@", NSStringFromCGRect(self.frame));
                     }
                     completion:^(BOOL finished){
                         NSLog(@"done");
                     }];
}
@end

The code to animate:

[rewardAnimationButton moveTo:CGPointMake(100, 100) duration:5.0 option:0];

Solution

  • Did you turn off autolayout for your XIB file/ storyboard? Supposedly auto layout takes over view placement, and interferes with animation.

    You might also try animating your button's center rather than the frame. The frame property is undefined if the view's transform is not identity.