开发过程当为了让应用更绚,就加入一些动画效果。CoreAnimation比较复杂,其实UIView的简单动画就可以满足我们应用开发。UIView支持的动画属性有,frame, center, bounds,transform, alpha. 什么意思呢,就是你可以修改这些属性来完成动画,比如设置frame,uiview就可以动了。下面看一段代码:
// begin the animations block:
[UIView beginAnimations:@"animationID" context:NULL];
// define how long the animation should take, in seconds. 0.5 is half a second.
[UIView setAnimationDuration:3];
// animate the view itself by changing its frame:
[theView setFrame:newFrame];
// or by changing its alpha value:
[theView setAlpha:0.5];
// or by rotating it:
theView.transform = CGAffineTransformMakeRotation(-0.7853981625); // 45 deg
// optional: to execute code after the animation has finished, set the delegate:
[UIView setAnimationDelegate:self];
// optional: set the animation curve:
// UIViewAnimationCurveEaseIn: begins slowly, then speeds up
// UIViewAnimationCurveEaseOut: begins fast, then slows down
// UIViewAnimationCurveEaseInOut: begins slowly, faster in the middle, slows down at end
// UIViewAnimationCurveLinear: same speed throughout
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
// start animating!
[UIView commitAnimations];
这样就完成了一个动画效果, beginAnimatins:与commitAnimations这是必须写的,中间设置你的属性,这儿设置了三个属性,分别是frame,alpha,transform. 这样就完成了一个动画,那个"animationID"随便取名字。动画的时间有多长呢, 有一个默认时间,在这儿我们设置的是3秒, 动画有一个快慢,比如快进,setAnimationCurve就是控制什么时候快进,什么时候慢播。当这个动画完了的时候,会有一个delegate方法调用:
- (void)animationDidStop:(NSString *)animID finished:(BOOL)didFinish context:(void *)context
{
if ( [animID isEqualToString:@"animationID"] ) {
NSLog(@"move10 animation finished.");
[self moveBack];
}
else if ( [animID isEqualToString:@"moveBack"] ) {
NSLog(@"done moving.");
}
}
在这儿你可以检测到哪一个动画完了,比如在这儿我们检测到了animationID与moveBack.
是不是很简单呀?
在iOS4以上,支持block了,所以用它使编程将更简单,如传统的动画是这样:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:containerView cache:YES];
[containerView addSubview:subview];
[UIView commitAnimations];
如果用block的方式将是:
[UIView transitionWithView:containerView duration:0.5
options:UIViewAnimationOptionTransitionCurlDown
animations:^ { [containerView addSubview:subview]; }
completion:nil];
具体参看文档: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html