repeating chained animation in swift

Refresh

March 2019

Views

841 time

1

the function here was write in order to animation the view as following : 2 sec fade out, 2 sec fade in , 2 sec delay and repeat. for some reason the animation preform only once and do not repeat. what am i doing wrong here ?

UIView.animateWithDuration(6.0,
            delay: 0.0,
            options: [.AllowUserInteraction,.Repeat,.BeginFromCurrentState],
            animations: {

            UIView.animateWithDuration(2.0,
                delay: 0.0,
                options: [.AllowUserInteraction,.BeginFromCurrentState] ,
                animations: {
                    //fade out
                    self.alpha = 0.5
                },
                completion: { finished in

                    UIView.animateWithDuration(2.0,
                        delay: 0.0,
                        options: [.AllowUserInteraction,.BeginFromCurrentState],
                        animations: {
                            //fade in

                            self.alpha = 1.0
                        },
                        completion: { finished in

                            UIView.animateWithDuration(2.0,
                                delay: 0.0,
                                options: [.AllowUserInteraction,.BeginFromCurrentState],
                                animations: {

                                },
                                completion: { finished in                                    
                            })                                                             
                   })                              
            })
        },
        completion: { finished in
    })
}

1 answers

2

You can use Core Animation delegates to repeat forever your animation, for example:

func animateCustomView(layer: CALayer) {
    let speed = 60.0 / Double(view.layer.frame.size.width)
    let duration: NSTimeInterval = Double(view.layer.frame.size.width - layer.frame.origin.x) * speed

    let move = CABasicAnimation(keyPath: "position.x")
    move.duration = duration
    move.toValue = self.view.bounds.size.width + layer.bounds.width / 2
    move.delegate = self
    move.setValue("view", forKey: "name")
    move.setValue(layer, forKey: "layer")

    layer.addAnimation(move, forKey: nil)
}

// Core Animation Delegate implementation

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {

    if let name = anim.valueForKey("name") as? String{
        if name == "view" {
            let layer = anim.valueForKey("layer") as? CALayer
            layer?.position.x = -(layer?.bounds.width)! / 2
            delay(seconds: 0.5, completion: { 
                self.animateCustomView(layer!)
            })
            // Here add more delays and in completion handler block, add your chained animations.
        } else if name == "other name" {
            // or you can reinitialize it your another chained animations, delay it and call it again.
        }
    }
}

override func viewDidAppear(animated: Bool) {
    animateCustomView(viewToAnimate.layer)
    // Here call your other chained animations
}

The content of the funcs are just code for example purpose, you can add your custom animation there.