Search code examples
iosswiftcore-animation

Swift Completely restart the animation on button click


so what I wanna do is to restart the animation, when it's completed and I pressed the Restart button.

Here's what I've got right now. It starts like that:

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var fillView: UIView!

    @IBAction func startAnimation(sender: AnyObject) {
        self.animationBackground(fillView, animationTime: Float(15))
    }

    @IBAction func restartAnimation(sender: AnyObject) {
        let layerRectangle = fillView.layer
       restartLayer(layerRectangle)
    }

    func animationBackground(view:UIView,animationTime:Float){
        UIView.animateWithDuration(NSTimeInterval(animationTime), delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: {
            view.transform = CGAffineTransformMakeTranslation(0.0,  self.screenSize.height-67)
        },completion:nil)
    }

    func restartLayer(layer:CALayer){
        layer.beginTime = 0.0
        layer.speed = 0.0        
    }
}

The restart function works only while animation is playing. But when it's completed that doesn't restart the animation from the beginning. Any tips on how to restart the animation on button click but only when it's already done?


Solution

  • I have already written above.. by the way:

    func animationBackground(view:UIView,animationTime:Float){
    
         self.fillView.transform = CGAffineTransformIdentity
    
         UIView.animateWithDuration(NSTimeInterval(animationTime), delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: {
                    view.transform = CGAffineTransformMakeTranslation(0.0,  self.screenSize.height-67)
            },completion:nil)
        }