1. 代码块方式实现动画;
example:
二、begin/commit 方式实现 nest 动画;
example :
2. begin/commit 方式实现nest 的动画;
一、代码块方式实现:
需要用到这几个API
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
/* Performs `animations` using a timing curve described by the motion of a spring. When `dampingRatio` is 1, the animation will smoothly decelerate to its final model values without oscillating. Damping ratios less than 1 will oscillate more and more before coming to a complete stop. You can use the initial spring velocity to specify how fast the object at the end of the simulated spring was moving before it was attached. It's a unit coordinate system, where 1 is defined as travelling the total animation distance in a second. So if you're changing an object's position by 200pt in this animation, and you want the animation to behave as if the object was moving at 100pt/s before the animation started, you'd pass 0.5. You'll typically want to pass 0 for the velocity. */
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
example:
[UIView animateWithDuration:1
animations:^(void){
airship.alpha = 1.0;
airship.transform = CGAffineTransformMakeTranslation(0, 20);
}
completion:^(BOOL finished){
}];
二、begin/commit 方式实现 nest 动画;
当我们需要同时实现一个UIView的缩放、位置、不透明度等属性的同时动画时,需要用到这种方法。
+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // additional context info passed to will start/did stop selectors. begin/commit can be nested
+ (void)commitAnimations; // starts up any animations when the top level animation is commited
example :
[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDidStopSelector:@selector(animationDidStop)];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
airship.alpha = 0.0;
CGRect rect = airship.bounds;
airship.bounds = CGRectMake(0, 0, rect.size.width/2.0, rect.size.height/2.0);
CGPoint center = airship.center;
airship.center = CGPointMake(center.x, 0);
[UIView commitAnimations];