在iPhone开发中沿着曲线移动对象

时间:2010-12-27 11:12:38

标签: iphone ipad core-animation

我希望通过沿着特定曲线移动图像对象来为其设置动画。它不是一般的或随机的曲线,而是跟随屏幕上特定路径的曲线。

目前,我通过每次设置其帧来手动指定我想要图像对象移动的路径的x和y坐标列表。在设置路径的特定x和y坐标并沿着它移动图像的意义上,这是一个费力的过程。有没有更有效的方法来做到这一点?

有没有一种方法可以指定,例如,大约15-20个点,并沿着那些移动物体的曲线进行跟踪?任何其他方式来实现这一点?任何帮助将非常感激。感谢。

2 个答案:

答案 0 :(得分:10)

您可以使用UIBezierPath和CAKeyFrameAnimation的组合。 我找到了一篇非常有用的博客文章来处理这个主题。

http://oleb.net/blog/2010/12/animating-drawing-of-cgpath-with-cashapelayer/

这是我使用的简化版本(它只是为正方形绘制动画):

UIBezierPath *customPath = [UIBezierPath bezierPath];
[customPath moveToPoint:CGPointMake(100,100)];
[customPath addLineToPoint:CGPointMake(200,100)];
[customPath addLineToPoint:CGPointMake(200,200)];
[customPath addLineToPoint:CGPointMake(100,200)];
[customPath addLineToPoint:CGPointMake(100,100)];

UIImage *movingImage = [UIImage imageNamed:@"foo.png"];
CALayer *movingLayer = [CALayer layer];
movingLayer.contents = (id)movingImage.CGImage;
movingLayer.anchorPoint = CGPointZero;
movingLayer.frame = CGRectMake(0.0f, 0.0f, movingImage.size.width, movingImage.size.height);
[self.view.layer addSublayer:movingLayer];

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 4.0f;
pathAnimation.path = customPath.CGPath;
pathAnimation.calculationMode = kCAAnimationLinear;
[movingLayer addAnimation:pathAnimation forKey:@"movingAnimation"];

答案 1 :(得分:0)

在@Jilouc的Swift-3版本中: -

    override func viewDidLoad() {
        super.viewDidLoad()
        addAdditiveAnimation()
        initiateAnimation()
     }

    //curve which follows a particular path  
    func pathToTrace() -> UIBezierPath {
            let path =  UIBezierPath(ovalIn: CGRect(x: 120  , y: 120, width: 100, height: 100))
            let shapeLayer = CAShapeLayer()
            shapeLayer.path = path.cgPath
            shapeLayer.strokeColor = UIColor.red.cgColor
            shapeLayer.lineWidth = 1.0
            self.view.layer.addSublayer(shapeLayer)
            return path
        }

func addAdditiveAnimation() {
        let movement = CAKeyframeAnimation(keyPath: "position")
        movement.path = pathToTrace().cgPath
        movement.duration = 5
        movement.repeatCount = HUGE
        movement.calculationMode = kCAAnimationPaced
        movement.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
        self.movement = movement
}

func createLayer() -> CALayer {
        let layer = CALayer()
        let image = UIImage(named: "launch.png")
        layer.frame = CGRect(x: 0 , y: 0, width: (image?.size.width),height: (image?.size.height))
        layer.position = CGPoint(x: 5, y: 5)
        layer.contents = image?.cgImage
        layer.anchorPoint =  .zero
        layer.backgroundColor =  UIColor.red.cgColor
        //layer.cornerRadius =  5
        self.view.layer.addSublayer(layer)
        return layer
    }

func initiateAnimation() {
        let layer = createLayer()
        layer.add(self.movement, forKey: "Object Movement")
     }

Github Demo