kgibbon
kgibbon

Reputation: 726

Smooth frame animating on UIView

I am trying to move a UIView around the screen by incrementing the UIView's x property in an animation block. I want the element to move continuously so I cannot just specify an ending x and up the duration.

This code works but it is very choppy. Looks great in the simulator but choppy on the device.

-(void)moveGreyDocumentRight:(UIImageView*)greyFolderView
{
[UIView animateWithDuration:0.05 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    NSInteger newX = greyFolderView.frame.origin.x + 5.0;
    greyFolderView.frame = CGRectMake(newX, greyFolderView.frame.origin.y, greyFolderView.frame.size.width, greyFolderView.frame.size.height);
    }
} completion:^(BOOL finished) {
    [self moveGreyDocumentRight:greyFolderView];
}];

}

Upvotes: 0

Views: 1970

Answers (1)

Rob Napier
Rob Napier

Reputation: 299605

You're fighting the view animation here. Each one of your animations includes a UIViewAnimationOptionCurveEaseInOut timing curve. That means that every 0.05 seconds you try to ramp up your speed then slow down your speed then change to somewhere else.

The first and simplest solution is likely to change to a linear timing by passing the option UIViewAnimationOptionCurveLinear.

That said, making a new animation every 5ms really fights the point of Core Animation, complicating the code and hurting performance. Send the frame it to the place you currently want it to go. Whenever you want it to go somewhere else (even if it's still animating), send it to the new place passing the option UIViewAnimationOptionBeginFromCurrentState. It will automatically adjust to the new target. If you want it to repeat the animation or bounce back and forth, use the repeating options (UIViewAnimationOptionRepeat and UIViewAnimationOptionAutoreverse).

Upvotes: 3

Related Questions