Animating the Visual Change

You may have noticed that as you move the slider toward the right, the animation slows and becomes choppy. To make the animation smoother, you can animate the visual changes by using one of the block-based animation methods. One such block-based animation method is the animateWithDuration:delay:options:animations:completion: class method of the UIView class:

[UIView animateWithDuration:slider.value
                      delay:0.0f
                    options:UIViewAnimationOptionAllowUserInteraction |
                            UIViewAnimationOptionCurveLinear
                 animations:^{
                     imageView.center = CGPointMake(imageView.center.x + delta.x,
                                                    imageView.center.y + delta.y);
                     }
                     completion:nil];

The preceding code performs the specified animations immediately using the UIViewAnimationOptionCurveLinear (constant speed) and UIViewAnimationOptionAllowUserInteraction (allows the user to interact with the views while they are being animated) animation options. This results in a much smoother animation.

Get Beginning iOS 5 Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.