Moving an object along a curve in iPhone development

后端 未结 2 1708
北海茫月
北海茫月 2020-12-14 04:20

I wanted to animate an image object by moving it along a particular curve. It is not a general or random curve but rather a curve which follows a particular path on screen.<

相关标签:
2条回答
  • 2020-12-14 05:17

    In Swift-3 version of @Jilouc :-

        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

    0 讨论(0)
  • 2020-12-14 05:23

    You could use a combination of UIBezierPath and CAKeyFrameAnimation. I found a very useful blog post dealing with this subject.

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

    Here's a simplified version of what I used (it just animates the drawing of a square):

    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"];
    
    0 讨论(0)
提交回复
热议问题