IOS----Core Animation介绍4

原文地址:http://superman474.blog.163.com/blog/static/120661462011857653190/

animation(动画)

     动画是用户界面中一个重要的部分,当我们使用core animation时,动画是完全自动的,不需要动画循环或者定时器,我们的程序不需要一帧一帧的去绘制,也不需要去跟踪动画当前的状态。动画发生在一个独立的线程里,不需要与应用程序交互。
     本篇文章就主要讲述一下动画类,以及描述一下如何创建明确的(explicit)动画和隐含的(implicit)动画。

1.animation类和timing(时序)
     core Animation提供了一个animation类的集合可以供我们应用程序使用。
     CABasicAnimation提供简单的layer属性值之间的interpolation。
     CAKeyframeAnimation提供了关键帧动画的支持。我们可以指定layer属性进行动画的关键路径,一个包含每一个阶段动画的数组。
     CATransition提供一个过度效果(影响到整个layer的内容),当动画时,它fade(淡化),push(推),或者reveals(揭露)layer的内容。此动画可以用自己提供的自定义 core image filters去扩展。
     CAAnimationGroup允许将一组动画放在一个数组里面,然后同时执行。

     除了指定具体的动画类型,我们必须要指定动画的持续时间, pacing等。动画可以重复,设置重复的次数,或者当动画周期完成时是否自动反转。animation类和CAMediaTiming协议提供了这里所有的功能。


2.implicit animation
    core animation的隐含动画假定所有改变的可动画的layer属性应该是渐进和异步的。改变一个可动画的layer属性值可以隐含的引起一个动画使属性的值从旧值变为新值。

下面一句话就是简单的触发一个隐含的动画时layer从当前的position到新的position。
// assume that the layer is current positioned at (100.0,100.0) 
theLayer.position=CGPointMake(500.0,500.0); 
也可以一次改变多个layer属性,或者同时改变多个layer的属性值都可以。下面代码说明了此。
// animate theLayer's opacity to 0 while moving it 
// further away in the layer 
theLayer.opacity=0.0; 
theLayer.zPosition=-100; 
  
// animate anotherLayer's opacity to 1 
//  while moving it closer in the layer 
anotherLayer.opacity=1.0; 
anotherLayer.zPosition=100.0; 
     隐含动画使用该属性使用的默认的动画中指定的时间。可以覆盖此默认的时间,在6中我们在介绍。


3.明确的动画
明确的动画需要我们去创建一个animation对象,设置开始和结束值。一个明确的动画不会自动开始除非你将其应用到layer中。下面的代码将创建一个明确的动画,在3秒中过度一个layer的opacity从opaque到full transparent(全部透明),并且在返回原样。直到将其加到layer上时,动画才开始。
CABasicAnimation *theAnimation; 
  
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; 
theAnimation.duration=3.0; 
theAnimation.repeatCount=2; 
theAnimation.autoreverses=YES; 
theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; 
theAnimation.toValue=[NSNumber numberWithFloat:0.0]; 
[theLayer addAnimation:theAnimation forKey:@"animateOpacity"]; 
   
     当想创建一个动画让其持续运行时,显式的动画是很有用的。


// The selection layer will pulse continuously. 
// This is accomplished by setting a bloom filter on the layer 
  
// create the filter and set its default values 
CIFilter *filter = [CIFilter filterWithName:@"CIBloom"]; 
[filter setDefaults]; 
[filter setValue:[NSNumber numberWithFloat:5.0] forKey:@"inputRadius"]; 
  
// name the filter so we can use the keypath to animate the inputIntensity 
// attribute of the filter 
[filter setName:@"pulseFilter"]; 
  
// set the filter to the selection layer's filters 
[selectionLayer setFilters:[NSArray arrayWithObject:filter]]; 
  
// create the animation that will handle the pulsing. 
CABasicAnimation* pulseAnimation = [CABasicAnimation animation]; 
  
// the attribute we want to animate is the inputIntensity 
// of the pulseFilter 
pulseAnimation.keyPath = @"filters.pulseFilter.inputIntensity"; 
  
// we want it to animate from the value 0 to 1 
pulseAnimation.fromValue = [NSNumber numberWithFloat: 0.0]; 
pulseAnimation.toValue = [NSNumber numberWithFloat: 1.5]; 
  
// over a one second duration, and run an infinite 
// number of times 
pulseAnimation.duration = 1.0; 
pulseAnimation.repeatCount = HUGE_VALF; 
  
// we want it to fade on, and fade off, so it needs to 
// automatically autoreverse.. this causes the intensity 
// input to go from 0 to 1 to 0 
pulseAnimation.autoreverses = YES; 
  
// use a timing curve of easy in, easy out.. 
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; 
  
// add the animation to the selection layer. This causes 
// it to begin animating. We'll use pulseAnimation as the 
// animation key name 
[selectionLayer addAnimation:pulseAnimation forKey:@"pulseAnimation"]; 

(上面的代码用到了过滤器)

4.启动和停止显式动画

     可以通过发送消息addAnimation:forKey:给layer来启动显式动画,传递动画类和标识作为参数。
     一旦加入,显式动画就会开始运行直到其动画完成,或者从layer上remove。标识就是用来停止一个动画的,removeAnimationForKey:,也可以通过removeAllAnimations停止一个layer所有的动画。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值