一、仿射变换--CGAffineTransform
1.仿射变换,一种基于二维坐标的转换功能。CGAffineTransform数据结构代表一个二维坐标系的转换矩阵。
2.通过CGAffineTransform数据结构,可以构造二维坐标系内的平移、伸缩、旋转矩阵。详情可以参看苹果开发文档中关于该数据结构的解释。
3.UIView类的一个属性:transform,该属性相对于bounds的中心位置,该属性的类型为CGAffineTransform。初始化一个CGAffineTransform结构的矩阵,然后赋值给这个属性,就可以完成二维坐标系内的转换。
4.要形成动画,可以使用第三部分(UIView集成动画API)表述的方式,通过指定该属性的前后状态来形成动画,或者自己动态设置这个属性的值来形成动画。
二、三维变换--CATransform3D
1.CATransform3D数据结构代表一个三维坐标系的矩阵,通过它可以构造三维坐标系内的平移、伸缩、旋转矩阵。
2.UIView类的layer.transform属性,该属性相对于bounds的中心位置。该属性的类型为CATransform3D。该属性的使用方式以及动画的形成,同上一部分,但是使用这个属性实现三维动画,可能没那么好控制。
三、UIView集成的动画API
1.UIView集成了一系列的动画API。她的原理类似于flash动画的制作,我们指定物体(控件)某些属性在动画中的开始和结束状态,动画API就会自动生成中间的帧,从而生成动画。
2.使用示例(iOS4之前):
//在这里指定控件的开始状态
//xxxxxxxxxx
[UIView beginAnimations:context:];
[UIView setAnimationDuration:];
[UIView setAnimationCurve:];
[UIView setAnimationDelegate:];
[UIView setAnimationWillStartSelector:@selector(animationDidStart:context)];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context)];
//在这里指定控件的结束状态
//xxxxxxxxx
[UIView commitAnimations];
代理方法的声明要使用如下方式:
(void)animationDidStart:(NSString *)animationID context:(void *)context
(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
3.使用示例(iOS4及以后)
(1)从iOS4系统之后,第2点提到整套API被废弃了,应该使用block的方式来生成动画。API列表如下:
animateWithDuration:delay:options:animations:completion:
animateWithDuration:animations:completion:
animateWithDuration:animations:
transitionWithView:duration:options:animations:completion:
transitionFromView:toView:duration:options:completion:
(2)使用此套API的时候,将控件的结束状态写在block中就可以了。
4.因为此系列API只指定动画的开始和结束状态,所以只能做一些有规律的动画,否则API会不知道如何生成中间帧。常用动画举例:平移,伸缩,旋转,透明度变化,颜色变化等等。
四、核心动画
1.比较常用的动画类:CATransition和CAAnimation。
(1)CATransition用于使用系统集成的动画。
(2)CAAnimation用于实现自定义动画,但是它是一个抽象类,不能直接使用,要子类化才能使用。类库中存在已经子类化的类可以使用,比如CAKeyframeAnimation,此类通过给一个layer的属性指定一组值,包含开始、结束以及中间的一些用来修正动画的值,然后根据这些值,生成每一帧,来生成动画。
2.CATransition动画实现示例:
CATransition *transition = [CATransition animation];
transition.duration = 0.8;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = @"cube";
transition.subtype = kCATransitionFromRight;
[xxxView.layer addAnimation:transition forKey:nil];
3.CAAnimation使用示例:
CGFloat perAngle = M_PI / 64;
CAKeyframeAnimation *keyFrameAnination = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
[keyFrameAnination setDuration:2.0f];
[keyFrameAnination setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[keyFrameAnination setRemovedOnCompletion:NO];
[keyFrameAnination setFillMode:kCAFillModeForwards];
[keyFrameAnination setDelegate:self];
NSMutableArray *values = [[NSMutableArray alloc]initWithCapacity:10];
for (CGFloat clipRdn = angle; clipRdn >= 0; clipRdn -= perAngle) {
CATransform3D transform = CATransform3DMakeRotation(clipRdn, 0.0f, 1.0f, 0.0f);
transform.m34 = sin(clipRdn) / 4000.0f;
transform.m14 = -sin(clipRdn) / 4000.0f;
[values addObject:[NSValue valueWithCATransform3D:transform]];
}
[keyFrameAnination setValues:values];
[values release];
[self.imageView.layer addAnimation:keyFrameAnination forKey:@"animation"];