HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

Upgrading animation code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
codeanimationupgrading

Problem

I understand that this animation code is outdated:

[UIView beginAnimations:@"Move" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelay:0.08];
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];


What are the modern best practices for achieving the same result?

Solution

In iOS 4 and later you are encouraged to use animation blocks.

[UIView animateWithDuration: 0.5f
            delay: 0.08f
            options: UIViewAnimationCurveEaseIn
            animations: ^{
                self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
            }
            completion: ^(BOOL finished){
               // any code you want to be executed upon animation completion
            }
];


One advantage of using block-based animation is that


When this code executes, the specified animations are started immediately on another thread so as to avoid blocking the current thread or your application’s main thread.

This means that the rest of your application will not be "locked up" while your animation is executing.

Code Snippets

[UIView animateWithDuration: 0.5f
            delay: 0.08f
            options: UIViewAnimationCurveEaseIn
            animations: ^{
                self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
            }
            completion: ^(BOOL finished){
               // any code you want to be executed upon animation completion
            }
];

Context

StackExchange Code Review Q#15835, answer score: 10

Revisions (0)

No revisions yet.