Search code examples
iosxcodeswiftcalayercaanimation

Swift : Pause CABasicAnimation for CALayer


The code you see below creates a CALayer(rectangle shape) and animates it from the left to the right(you can copy and paste code directly in new project):

     //Global Variables
var layer = CALayer()
var holdGesture = UILongPressGestureRecognizer()
let animation = CABasicAnimation(keyPath: "bounds.size.width")


func setUpView(){

    self.view.addGestureRecognizer(holdGesture)
    holdGesture.addTarget(self, action:"handleLongPress:")

}


func handleLongPress(sender : UILongPressGestureRecognizer){

    //NEED IT HERE
    //var layer = CALayer()

    layer.frame = CGRect(x: 0, y: 0, width: 0, height: 10)
    layer.backgroundColor = UIColor.redColor().CGColor

    animation.fromValue = 0
    animation.toValue = self.view.bounds.width * 2
    animation.duration = 5
    self.view.layer.addSublayer(layer)

    if(sender.state == .Began){
        print("Long Press Began")
        layer.addAnimation(animation, forKey: "bounds.size.width")
    }


    else{
        print("Long press ended")
        pauseLayer(layer)
    }

}

func pauseLayer(layer : CALayer){
    let pausedTime : CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
    layer.speed = 0.0
    layer.timeOffset = pausedTime

}



override func didReceiveMemoryWarning(){
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    setUpView()

}

The issue I'm having is the variable "layer" can only be paused(when passed through the "pauseLayer" function) if it is a global variable! I do not know why! I want to declare the variable within the "handleLongPress" function.The reason for this is because i need to declare a new variable with the same name every time the longPressGestureRecognizer is recognized. Ive tried pass by reference with "inout" but it didn't seem to work. Can someone help? Please.


Solution

  • Use this Modified code:

    //Global Variables
    var layer: CALayer?
    var holdGesture = UILongPressGestureRecognizer()
    let animation = CABasicAnimation(keyPath: "bounds.size.width")
    
    
    func setUpView(){
    
        self.view.addGestureRecognizer(holdGesture)
        holdGesture.addTarget(self, action:"handleLongPress:")
    
    }
    
    
    func handleLongPress(sender : UILongPressGestureRecognizer){
    
        if(sender.state == .Began) {
    
            let newLayer = CALayer()
            newLayer.frame = CGRect(x: 0, y: 0, width: 0, height: 10)
            newLayer.backgroundColor = UIColor.redColor().CGColor
    
            animation.fromValue = 0
            animation.toValue = self.view.bounds.width * 2
            animation.duration = 5
            self.view.layer.addSublayer(newLayer)
    
            print("Long Press Began")
            newLayer.addAnimation(animation, forKey: "bounds.size.width")
    
            layer = newLayer
        }
        else {
            print("Long press ended")
    
            if let layer = layer {
                pauseLayer(layer)
                layer.removeFromSuperlayer()
            }
        }
    
    }
    
    func pauseLayer(layer : CALayer){
        let pausedTime : CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        layer.speed = 0.0
        layer.timeOffset = pausedTime
    
    }