iOS动画--基础
2016-05-13 本文已影响856人
CatchZeng
动画
所谓动画就是使一幅图像“活”起来的过程。当眼睛看到多张图片连续快速的切换时,就会被认为是一段连续播放的动画。
实验证明:动画和电影的画面刷新率为24帧/s,即每秒放映24幅画面,则人眼看到的是连续的画面效果。
CoreAnimation分为三大类,分别是CAPropertyAnimation(属性动画)、CAAnimationGroup(动画组)、CATransition(转场动画)。
属性动画
CAPropertyAnimation:属性动画的基类(通过可动画属性进行动画设置),不能直接使用。
CABasicAnimation:基础动画,通过属性修改进行动画参数控制,只有初始状态和结束状态(两个关键帧)。
CAKeyframeAnimation:关键帧动画,同样是通过属性进行动画参数控制,但是同基础动画不同的是它可以有多个状态控制。
动画组
CAAnimationGroup:动画组,动画组是一种组合模式设计,可以通过动画组来进行所有动画行为的统一控制,组中所有动画效果可以并发执行。
转场动画
CATransition:转场动画,主要通过滤镜进行动画效果设置。
CoreAnimation实例
CABasicAnimation
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.duration=5.0f;
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
scaleAnimation.fromValue = @(1);
scaleAnimation.toValue = @(1.5);
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.removedOnCompletion = NO;
[_aniView.layer addAnimation:scaleAnimation forKey:@"transformscale"];
CABasicAnimation
CAKeyframeAnimation
CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
NSValue *key1=[NSValue valueWithCGPoint:_aniView.layer.position];
NSValue *key2=[NSValue valueWithCGPoint:CGPointMake(100,100)];
NSValue *key3=[NSValue valueWithCGPoint:CGPointMake(200,100)];
NSValue *key4=[NSValue valueWithCGPoint:CGPointMake(200,500)];
NSArray *values=@[key1,key2,key3,key4];
keyframeAnimation.values=values;
keyframeAnimation.duration=8.0;
keyframeAnimation.beginTime=CACurrentMediaTime();
[_aniView.layer addAnimation:keyframeAnimation forKey:@"KCKeyframeAnimation_Position"];
CAKeyframeAnimation
CAAnimationGroup
-(void)groupAnimation{
CAAnimationGroup *animationGroup=[CAAnimationGroup animation];
CABasicAnimation *basicAnimation=[self rotationAnimation];
CAKeyframeAnimation *keyframeAnimation=[self translationAnimation];
animationGroup.animations=@[basicAnimation,keyframeAnimation];
animationGroup.delegate=self;
animationGroup.duration=10.0;
animationGroup.beginTime=CACurrentMediaTime();
[_aniView.layer addAnimation:animationGroup forKey:nil];
}
-(CABasicAnimation *)rotationAnimation{
CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
CGFloat toValue=M_PI_2*3;
basicAnimation.toValue=[NSNumber numberWithFloat:M_PI_2*3];
basicAnimation.autoreverses=true;
basicAnimation.repeatCount=HUGE_VALF;
basicAnimation.removedOnCompletion=NO;
[basicAnimation setValue:[NSNumber numberWithFloat:toValue] forKey:@"KCBasicAnimationProperty_ToValue"];
return basicAnimation;
}
-(CAKeyframeAnimation *)translationAnimation{
CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
NSValue *key1=[NSValue valueWithCGPoint:_aniView.layer.position];
NSValue *key2=[NSValue valueWithCGPoint:CGPointMake(100,100)];
NSValue *key3=[NSValue valueWithCGPoint:CGPointMake(200,100)];
NSValue *key4=[NSValue valueWithCGPoint:CGPointMake(200,500)];
NSArray *values=@[key1,key2,key3,key4];
keyframeAnimation.values=values;
[keyframeAnimation setValue:[NSValue valueWithCGPoint:_aniView.layer.position] forKey:@"KCKeyframeAnimationProperty_EndPosition"];
return keyframeAnimation;
}
CAAnimationGroup
CATransition
CATransition *animation = [CATransition animation];
animation.duration = 5.0f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = kCATransitionMoveIn;
animation.subtype = kCATransitionFromTop;
[_aniView.layer addAnimation:animation forKey:nil];
transition
基础部分就先讲到这了,下一篇开始将讲下较复杂、炫酷的动画实现。