要实现动画的所设置的路径代码:
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor redColor];
[color set];
_aPath = [UIBezierPath bezierPath];
[_aPath addArcWithCenter:CGPointMake(self.center.x, self.center.y-50) radius:50 startAngle:M_PI_2 endAngle:M_PI*2.f*3/4 clockwise:YES];
[_aPath addArcWithCenter:CGPointMake(self.center.x, self.center.y) radius:100 startAngle:-M_PI_2 endAngle:M_PI*2.f*3/4 clockwise:YES];
[_aPath addArcWithCenter:CGPointMake(self.center.x, self.center.y) radius:100 startAngle:-M_PI_2 endAngle:M_PI*2.f*3/4/3 clockwise:YES];
[_aPath addArcWithCenter:CGPointMake(self.center.x, self.center.y+50) radius:50 startAngle:M_PI_2 endAngle:M_PI*2.f*3/4 clockwise:YES];
_aPath.lineWidth = 5.0;
_aPath.lineCapStyle = kCGLineCapRound; //线条拐角
_aPath.lineJoinStyle = kCGLineCapRound; //终点处理
[_aPath stroke];
}
下面的是我执行动画代码:
CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
keyframeAnimation.path = dr.aPath.CGPath;
keyframeAnimation.repeatCount=1;
keyframeAnimation.removedOnCompletion = NO;
keyframeAnimation.fillMode = kCAFillModeForwards;
keyframeAnimation.duration = 4.0f;
keyframeAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
keyframeAnimation.delegate=self;
[imageView.layer addAnimation:keyframeAnimation forKey:nil];
在执行动画的时候,动画出现卡顿
如果“卡顿”指的是它中间会停下来的话,那是因为插值方式。默认的插值方式是kCAAnimationLinear,关键帧直线相连进行插值。要想让动画连续进行,可以简单加一句:
keyframeAnimation.calculationMode = kCAAnimationPaced;
有更精细的需求,可以自己用keyTimes和timingFunctions来控制。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。