iOS动画-基础动画

来源:互联网 发布:红圈软件 编辑:程序博客网 时间:2024/06/11 21:09

iOS动画-基础动画

iOS基础动画,基础动画,利用系统封装的
[UIView animateWithDuration:0.3 animations:(void (^)(void))animations];
(iOS 7.0 里面多了一个
+(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion 弹簧函数,效果不错) 方法,在animation block里面直接修改属性值,即可线性插值完成动画。基本上所有的属性值都可以做到。

先举几个例子

case 1://@"位置大小"        {            _animationView.transform = CGAffineTransformIdentity;            CGRect frame = _originFrame;            frame.size.width = 60.;            frame.size.height = 60.;            [UIView animateWithDuration:0.3 animations:^{                self.animationView.frame = frame;            }];        }            break;        case 2://@"旋转"        {            CGAffineTransform transForm = CGAffineTransformMakeRotation(M_PI_4);            [UIView animateWithDuration:0.3 animations:^{                self.animationView.transform = transForm;            }];            break;        }        case 3://@"位移"        {            CGPoint center = CGPointMake(0, 0);            [UIView animateWithDuration:0.3 animations:^{                self.animationView.center = center;            }];        }            break;        case 4://@"颜色"        {            [UIView animateWithDuration:0.3 animations:^{                self.animationView.backgroundColor = [UIColor whiteColor];            }];        }            break;        case 5://@"缩放"        {            [UIView animateWithDuration:0.3 animations:^{                self.animationView.transform = CGAffineTransformMakeScale(1.3, 1.4);            }];        }            break;        case 6://@"layer动画"        {            [UIView animateWithDuration:0.3 animations:^{                self.animationView.layer.bounds = CGRectMake(0, 0, 40, 40);            }];        }

这儿提一下里面出现的CGAffineTransform
CoreGraphics里面定义的一个结构体,点击进去看到如下:

struct CGAffineTransform {  CGFloat a, b, c, d;  CGFloat tx, ty;};

6个变量 分别代表什么呢?
在了解这个之前先分别了解下面几个基于CGAffineTransform 的结构体

/* Return a transform which translates by `(tx, ty)’:
t’ = [ 1 0 0 1 tx ty ] */
CGAffineTransformMakeTranslation(CGFloat tx,
CGFloat ty)

位移依赖于: tx,ty两个变量

/* Return a transform which scales by `(sx, sy)’:
t’ = [ sx 0 0 sy 0 0 ] */
CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
缩放依赖于: a,d两个变量

/* Return a transform which rotates by `angle’ radians:
t’ = [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] */
CGAffineTransformMakeRotation(CGFloat angle)
翻滚则依赖于:a,b,c,d四个变量

可以简单的定义一下:tx,ty,掌握着target相对于目前位置的位移量,数值范围[-n,n];
以本身为坐标原点,屏幕向右为 X轴正方向,屏幕向下为Y轴正方向。tx为正,即向右移动,
反之向左移动, ty是一个道理。

a,b,c,d四个值中a,d两个值分别代表 宽和高的缩放量,一般来说缩放量就是一个正数吧,
比如a = 0.5,那么表现出来就是宽变为原来的0.5倍,a = 1.5的话就代表宽为原来的1.5倍
但是事实上,缩放量还可以为负,a = -1会表现出来什么结果呢?运行下面代码:

            transform.a = 1;            [UIView animateWithDuration:0.3 animations:^{                self.animationView.transform = transform;            }];

图片1

是的,X方向出现了翻转,也就是说,当缩放量为负的时候,是在其相反的方向上面进行缩放!
现在就只有b,c两个变量没有解释了,那是因为单独拿出来说是没有任何效益的,
a,b,c,d相互配合做旋转效果,比如b = 0.5,c =-0.5

CGAffineTransform transform = CGAffineTransformIdentity;            transform.b = 0.5;            transform.c = -0.5;            [UIView animateWithDuration:0.3 animations:^{                self.animationView.transform = transform;            }];

效果如下
图片2

0.5 = sin(30°),即target顺时针旋转了30°。这儿因为没有去设置a,d,都用的默认的1,然后图片会有一些放大,那是因为cos(30°) = 0.8 小于1的,这儿设置了1,就会产生宽高的缩放。

那么对于默认的变量:CGAffineTransformIdentity,初始值为[1,0,0,1,0,0]就很好解释了

至于transform为什么是一个向量,以后的高级篇幅里面,我会尝试解释一下,希望能解释的清楚。
这儿再说一个小窍门,就是一般情况下不需要你对a,b,c,d,tx,ty进行设置,可以直接去使用系统提供的方法进行组合,因为有以下系统封装方法:
CGAffineTransformTranslate(CGAffineTransform t,
CGFloat tx, CGFloat ty)
CGAffineTransformScale(CGAffineTransform t,
CGFloat sx, CGFloat sy)
CGAffineTransformRotate(CGAffineTransform t,
CGFloat angle)

多用用就理解了。

总体来说,本篇文章很简单,就说了一些简单的动画实现,另外简单的解释了一下CGAffineTransform,非常简单实用,你不需要知道其他东西,即可做出一些简单的动画。

原创粉丝点击