曲线核心图形的路径动画

时间:2016-11-19 18:09:07

标签: ios swift core-graphics

我有一个UIView,我想要在弯曲的路径上制作动画,类似于下载飞到Safari的停靠点。我不确定该怎么做,因为我确信它涉及Core Graphics和Core Animation。我找到的最后一个答案是从2011年开始使用iOS 6中不推荐使用的功能,但无法按原样运行。提前致谢。

2 个答案:

答案 0 :(得分:0)

你希望CAKeyframeAnimation具有一个可以动画的路径属性。

这里还有一个例子,它的mac和它的目标C.如果你想不通,我可以稍后发布一个Swift 3的例子: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html

答案 1 :(得分:0)

让我们取曲线点p1,p2,p3,p4和p5,&找到每对相邻点的中点。将m1标记为p1和p2的中点。同样适用于m2,m3,m4。

  • 将四边形曲线添加到点m2,以p2作为控制点。
  • 将四边形曲线添加到m3点,以p3作为控制点。
  • 将四边形曲线添加到点m4,以p4作为控制点。

代码:

CGFloat screenWidth = self.view.frame.size.width;
CGFloat screenHeight = self.view.frame.size.height;

UIView *aniView = [[UIView alloc] initWithFrame:CGRectMake(50, screenHeight, 50, 50)];
[aniView setBackgroundColor:[UIColor redColor]];
aniView.layer.cornerRadius = 25.0;
[self.view addSubview:aniView];


UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:aniView.center];
[movePath addQuadCurveToPoint:CGPointMake(screenWidth-50,screenHeight-50)
                 controlPoint:CGPointMake(screenWidth/2,screenHeight-150)];
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = YES;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim,  nil];
animGroup.duration = 2.0;

[CATransaction begin]; {
    [CATransaction setCompletionBlock:^{
        [aniView.layer removeAllAnimations];
        [aniView removeFromSuperview];
    }];

    [aniView.layer addAnimation:animGroup forKey:nil];

} [CATransaction commit];

将上面的代码复制到某个方法中并尝试调用方法...