简介:
1、iOS动画主要是指Core Animation框架,只要能在屏幕上显示出来的都可以设置动画。任何复杂的动画其实都是由一个个简单的动画组装而成的,只要我们善于分解和组装
2、动画类型:基础动画、组动画、过渡动画、关键帧动画、仿射动画和3D动画。
3、优点:Core Animation将大部分实际的绘图任务交给了图形硬件来处理,图形硬件会加速图形渲染的速度。这种自动化的图形加速技术让动画拥有更高的帧率并且显示效果更加平滑,不会加重CPU的负担而影响程序的运行速度。
调用方式:
UIView 代码块调用:一般在我们执行完动画后,还需要做其他事情,这就要用到UIView代码块调用。
[UIView animateWithDuration:1.0f animations:^{
} completion:^(BOOL finished) {
}];
UIView [begin commit]模式:这种方式会提供一个动画的选择器,可以选择在某某之前会之后等执行、结束动画等,【就是可以选择参照物】
[UIView beginAnimations:nilcontext:nil];
[UIView setAnimationDuration:0.6];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[UIView commitAnimations];
给View的Layer添加动画模式:这个也是基本的通用类型的,是Core Animation提供的方法。
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"position"];
anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-75)];
anima.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-75)];
anima.duration = 1.0f;
[_demoView.layer addAnimation:anima forKey:@"positionAnimation"];
动画方式:(这里只介绍基本动画和组动画)
基础动画(CABaseAnimation):重要属性fromValue: keyPath对应的初始值toValue: keyPath对应的结束值
基础动画主要提供了对于CALayer对象中的可变属性进行简单动画的操作。比如:位移、透明度、缩放、旋转、背景色等等。
注意:基础动画的每个动画都需要建立自己的动画对象,因为每个动画都有自己的animationWithKeyPath,【属性值直接对应animationWithKeyPath】。
组动画(CAAnimationGroup):分为同步、异步。。。把我们定义的动画加入到组动画的数组中。
同步组动画
CAKeyframeAnimation *anima1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-50)];
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2-50)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2+50)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2+50)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2-50)];
NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50)];
anima1.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4,value5, nil];
//缩放动画
CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
anima2.fromValue = [NSNumber numberWithFloat:0.8f];
anima2.toValue = [NSNumber numberWithFloat:2.0f];
//旋转动画
CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anima3.toValue = [NSNumber numberWithFloat:M_PI*4];
//组动画
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
groupAnimation.animations = [NSArray arrayWithObjects:anima1,anima2,anima3, nil];
groupAnimation.duration = 4.0f;
[_demoView.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
异步动画
//缩放动画(基础动画)【包括:位移、透明度、伸缩、旋转、背景色】
CFTimeIntervalcurrentTime =CACurrentMediaTime();
CABasicAnimation*anima1 = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];
anima1.fromValue= [NSNumbernumberWithFloat:0.8f];
anima1.toValue= [NSNumbernumberWithFloat:3.0f];
anima1.beginTime= currentTime;
anima1.duration=4.0;
[cell.layeraddAnimation:anima1forKey:@"anima1"];
//透明度(基础动画)
CABasicAnimation*anima2 = [CABasicAnimationanimationWithKeyPath:@"opacity"];
anima2.fromValue= [NSNumbernumberWithFloat:1.0f];
anima2.toValue= [NSNumbernumberWithFloat:0.0f];
anima2.beginTime= currentTime+4.0f;
anima2.duration=2.0f;
[cell.layeraddAnimation:anima2forKey:@"anima2"];
动画类型animationWithKeyPath:
position:位移
[_demoView.layeraddAnimation:animaforKey:@"positionAnimation"];
opacity:透明度
[_demoView.layeraddAnimation:animaforKey:@"opacityAniamtion"];
transform.scale:缩放动画
[_demoView.layeraddAnimation:animaforKey:@"scaleAnimation"];
transform.rotation.z:旋转
[_demoView.layeraddAnimation:animaforKey:@"rotateAnimation"];
backgroundColor:背景色变化
[_demoView.layeraddAnimation:animaforKey:@"backgroundAnimation"];
综合动画分析:(主要是借鉴的yixiangboy大神的)
最后推荐一个动画开源库:http://www.jianshu.com/p/69449e6bdc14
来到快一周了:如果你感觉这里还不错,加个好友吧!也非常感谢来 iOS传道 投稿!非常感谢啦!