加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 具有贝塞尔曲线和恒定速度的CAKeyframeAnimation

发布时间:2020-12-16 07:27:47 所属栏目:百科 来源:网络整理
导读:我试图沿着包含一些贝塞尔曲线的CGPathRef动画图形,我希望动画从开始到结束具有平滑,恒定的速度.我遇到的是,每次碰到一个关键点时,动画就会缓慢进出.我尝试过使用不同的timingFunction和calculateMode设置,但我似乎无法找到合适的组合来完成工作. 这是我到目
我试图沿着包含一些贝塞尔曲线的CGPathRef动画图形,我希望动画从开始到结束具有平滑,恒定的速度.我遇到的是,每次碰到一个关键点时,动画就会缓慢进出.我尝试过使用不同的timingFunction和calculateMode设置,但我似乎无法找到合适的组合来完成工作.

这是我到目前为止所得到的:

-(void)animate
{
  CAKeyframeAnimation *posAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  posAnim.path = [self pathForAnimation];
  posAnim.duration = 12.0;
  posAnim.calculationMode = kCAAnimationPaced;
  posAnim.fillMode = kCAFillModeBoth;
  posAnim.removedOnCompletion = NO;
  posAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  [image.layer addAnimation:posAnim forKey:@"posAnim"];
}

-(CGPathRef)pathForAnimation
{
  CGMutablePathRef path;
  CGPoint point;
  CGPoint controlPoint1;
  CGPoint controlPoint2;

  path = CGPathCreateMutable();
  point = CGPointMake(209.5f,24.5f);
  CGPathMoveToPoint(path,NULL,point.x,point.y);

  point = CGPointMake(249.5f,64.5f);
  controlPoint1 = CGPointMake(232.5f,25.5f);
  controlPoint2 = CGPointMake(249.5f,37.5f);
  CGPathAddCurveToPoint(path,controlPoint1.x,controlPoint1.y,controlPoint2.x,controlPoint2.y,point.y);

  point = CGPointMake(146.5f,301.5f);
  controlPoint1 = CGPointMake(249.5f,91.5f);
  controlPoint2 = CGPointMake(147.5f,258.5f);
  CGPathAddCurveToPoint(path,458.5f);
  controlPoint1 = CGPointMake(145.5f,344.5f);
  controlPoint2 = CGPointMake(170.5f,418.5f);
  CGPathAddCurveToPoint(path,point.y);

  point = CGPointMake(18.5f,530.5f);
  controlPoint1 = CGPointMake(122.5f,498.5f);
  controlPoint2 = CGPointMake(18.5f,530.5f);
  CGPathAddCurveToPoint(path,point.y);

  return path;
}

为了它的价值,我尝试用直线替换曲线(使用CGPathAddLineToPoint),这给了我一个像我想要的恒定速度.但当然,如果可能,我宁愿拥有曲线.

我也尝试用timingFunctions数组替换timingFunction,但无济于事.

解决方法

您是否尝试将关键帧动画的calculateMode属性设置为kCAAnimationCubicPaced?该属性专门用于使动画遵循整个曲线的恒定步调.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读