iPhone 实现动画效果
iPhone中实现动画,主要有两种方式:UIView的动画块和Core Animation的CATransition类。
1、UIView的动画块
之所以称为动画块,是因为UView动画是成块运行的,也就是说作为完整的事务一次性运行。
beginAnimation:context:标志动画块开始;
commitAnimations标志动画块结束。(这个commit多少已经暗示这个操作是事务性的)
这里面通常涉及4个操作:
beginAnimation:context:标志动画块开始
setAnimationCurve:定义动画加速或减速的方式,有四种,ease-in/ease-out,ease-in,linear,ease-out
setAnimationDuration:定义动画持续时间(以秒为单位)
commitAnimations:标志动画块结束
所有这些操作都是针对UIView的,或者说是UIView的类函数。
给段代码示例:
1. CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
[self.imageView setTransform:CGAffineTransformMakeScale(0.25f, 0.25f)];
[UIView commitAnimations];
(这里面设置了动画的delegate,在动画结束后执行animationFinished:函数)
UIView除了实现上面这种简单的动画,还支持视图的翻转。例如在上面代码的[UIView commitAnimations]前加上下面这句,便可以实现视图的翻转(翻转后的试图中,imageView的大小变为原来的0.25倍):
[UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromLeftforView:self.viewcache:YES];
其中,参数UIViewAnimationTransitionFlipFromLeft定义了翻转的方式。

浙公网安备 33010602011771号