概述
CABasicAnimation
- (void)demoViewBasicAnimation
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(_demoView.center.x, _demoView.center.y)]; //可以省略...
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(270, 410)];
animation.duration = 3.0f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; //动画速度设置
[_demoView.layer addAnimation:animation forKey:nil];
}
编译运行,会发现动画在执行后demoView回到了起点,这里需要对动画的两个属性进行设置,让图层在完成动画后停留在动画后的状态。
......
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
......
加入这两行代码后就简单的完成了位移动画
CAKeyframeAnimation
- (void)demoViewKeyframeAnimation
{
_demoView.layer.anchorPoint = CGPointMake(0.5, 0.0);
CAKeyframeAnimation *animaiton = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
NSArray *rotationVelues = @[@(M_PI_4), @(-M_PI_4), @(M_PI_4)];
animaiton.values = rotationVelues;
animaiton.duration = 3.0f;
animaiton.repeatCount = HUGE_VALF; // #define HUGE_VALF 1e50f
[_demoView.layer addAnimation:animaiton forKey:nil];
}
编译运行后可以发现demoView像一个钟摆一样不停摇晃,如果想效果更逼真,可以使用连续动画配合timingFunction来实现。
动画添加贝塞尔路径
- (void)demoViewBezierPathAnimation
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:_demoView.center]; //一定要设置 不然底层的CGPathRef找不到起始点,将会崩溃
[path addCurveToPoint:CGPointMake(270, 410) controlPoint1:CGPointMake(0, Screen_Height) controlPoint2:CGPointMake(Screen_Width, 0)]; //以左下角和右上角为控制点
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path.CGPath;
animation.duration = 3.0f;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[_demoView.layer addAnimation:animation forKey:nil];
}
Demo源码 点击打开链接
1486



被折叠的 条评论
为什么被折叠?



