ios动画 全面整理(UIVIew动画)
许多项目需要动画,每次动画都是到网上找,所以想把大多数的动画整理一遍,并且联系一遍,做个demo
ios动画主要分UIView动画、核心动画、转场动画、帧动画。
本篇主要总结一下UIView动画的一些知识。
UIView动画主要分为两种实现,一种是block实现,用的时候很方便,实现简单的动画很快,在UIView(UIViewAnimationWithBlocks) 类。一种是普通的方法,代码相对很多,在UIView(UIViewAnimation)
block动画里面,主要定义的是动画的时间,重复,延时,UIViewAnimationOptions动画选项(具体就可以看看这篇(博客园·小八究):http://www.cnblogs.com/xiaobajiu/p/4084747.html)等。
在普通方法中可以设置
[UIView setAnimationDuration:.5];//持续时间
[UIView setAnimationDelegate:self];//代理
[UIView setAnimationWillStartSelector:@selector(animationDoing)];//设置动画将开始时代理对象执行的SEL
[UIView setAnimationDidStopSelector:@selector(animationStop:)];//动画结束时代理对象执行的SEL
[UIView setAnimationDelay:0];//设置动画延迟执行的时间
[UIView setAnimationRepeatCount:2];//动画重复次数
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置动画曲线
[UIView setAnimationRepeatAutoreverses:YES];//动画是否执行相反的动画
等。
在动画里面需要设置UIView变化的参数,有frame、bounds、center中心、transform形变(包括平移、旋转、缩放、翻页( [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:manImageView cache:YES]; ))、alpha透明度、backgroundColor颜色、contentStretch内容拉伸模式参数。
transform 处理形变的,其可以改变控件的平移、缩放、旋转
CGAffineTransformMakeTranslation实现以初始位置为基准,在x轴方向上平移x单位,在y轴方向上平移y单位
当tx为正值时,会向x轴正方向平移,反之,则向x轴负方向平移;当ty为正值时,会向y轴正方向平移,反之,则向y轴负方向平移
CGAffineTransformMakeScale实现以初始位置为基准,在x轴方向上缩放x倍,在y轴方向上缩放y倍
CGAffineTransformMakeRotation实现以初始位置为基准,将坐标系统逆时针旋转angle弧度(弧度=π/180×角度,M_PI弧度代表180角度)
CGAffineTransformTranslate实现以一个已经存在的形变为基准,在x轴方向上平移x单位,在y轴方向上平移y单位
CGAffineTransformScale实现以一个已经存在的形变为基准,在x轴方向上缩放x倍,在y轴方向上缩放y倍
CGAffineTransformRotate实现以一个已经存在的形变为基准,将坐标系统逆时针旋转angle弧度(弧度=π/180×角度,M_PI弧度代表180角度)
block动画在执行是直接写一个blcok,填入需要设置的动画参数,然后再block内设置需要改变的UIView的属性即可,如,
[UIView animateWithDuration:.2 animations:^{
button.transform = CGAffineTransformMakeRotation(-M_PI_4);//旋转角度
} completion:^(BOOL finished) {
button.transform = CGAffineTransformRotate(button.transform,M_PI_4);
}];//一个晃动的小动画
普通动画执行,需要先调用开始执行的方法 + (void)beginAnimations:(nullable NSString *)animationID context:(nullable void *)context; // additional context info passed to will start/did stop selectors. begin/commit can be nested
[UIView beginAnimations:@"rotate" context:nil];
并且给动画设置一个animationID,然后设置动画的一些参数,设置需要变化的UIView的属性。
最后动画结束,需要调用结束方法+ (void)commitAnimations; // starts up any animations when the top level animation is commited
[UIView commitAnimations];
具体的代码如下,也是一个晃动的小动画,不过动画的开始和结束,都太生硬,需要进行优化,不过我用的是核心动画
//UIView动画
[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:.5];//持续时间
[UIView setAnimationDelegate:button];//代理
//设置动画将开始时代理对象执行的SEL
[UIView setAnimationWillStartSelector:@selector(animationDoing)];
[UIView setAnimationDidStopSelector:@selector(animationStop:)];
[UIView setAnimationDelay:0];//设置动画延迟执行的时间
//重复次数
[UIView setAnimationRepeatCount:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置动画曲线
//设置动画是否继续执行相反的动画
[UIView setAnimationRepeatAutoreverses:YES];
// button.transform = CGAffineTransformMakeScale(1.5, 1.5);//大小变化
button.transform = CGAffineTransformMakeRotation(M_PI_4);//旋转角度
[UIView commitAnimations];
button.transform = CGAffineTransformIdentity;//复位
//核心动画
CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimation animation];
keyAnimaion.keyPath = @"transform.rotation";
keyAnimaion.values = @[@(0),@(M_PI_4),@(0),@(-M_PI_4),@(0)];//度数转弧度
keyAnimaion.removedOnCompletion = NO;
keyAnimaion.fillMode = kCAFillModeForwards;
keyAnimaion.duration = 0.3;
keyAnimaion.repeatCount = 2;
[button.layer addAnimation:keyAnimaion forKey:nil];
浙公网安备 33010602011771号