简单的平移动画

      视图从原始位置移动到指定位置,一般而言,直接用frame重新设置它的位置,但是这样有一个不好的地方就是闲的太生硬了,所以我们应该考虑给它加点动画效果,这样看起来会比较流畅。

    1. 弹簧效果:CASpringAnimation 类

     这个是在iOS 9 之后才出现的,项目适用对象如果是8以上版本可以选择其它方式。

  代码如下:

       CASpringAnimation *spring = [CASpringAnimation animationWithKeyPath:@"position"];

       spring.damping = 5;//阻尼系数,值越大,停止越快

       spring.stiffness = 100;

       spring.mass = 1;

       spring.initialVelocity = 0;

       spring.fromValue = [NSValue valueWithCGPoint:fromValue];

       spring.toValue = [NSValue valueWithCGPoint:toValue];

       spring.duration = [spring settlingDuration];

注释:fromValue(原始位置CGPoint), toValue(指定位置CGPoint)

调用:[视图名.layer addAnimation:spring forkey:spring.keyPath];

    2.纵向平移(y)

   这里使用的是CABasicAnimation类,它是基础动画类。

代码如下:

       CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];

       animation.fromValue = [NSValue valueWithCGPoint:fromValue];

       animation.toValue = [NSValue valueWithCGPoint:toValue];

       animation.duration = 0.5;//持续时间

       animation.removedOnCompletion = NO;//运行一次是否移除动画

       animation.fillMode = kCAFillModeForwards;//动画结束后,layer会一直保持着动画最后的状态 

注释:fromValue和toValue同上


希望文章对你有小小的帮助。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容