Search code examples
iosswift3calayercatransform3d

Transform doesn't work with animation


I'm trying to set up a CALayer to increase in width at the bottom of my viewcontroller by using CATransform3DMakeScale. I can get the layer to scale just fine, but when I try to apply the transformation through an animation, the layer transforms without any animation.

let progressBar1 = CALayer()

override func viewDidAppear() {
    progressBar1.bounds = CGRect(x: 0, y: 0, width: 1, height: 5)
    progressBar1.position = CGPoint(x: 0, y: 600)
    progressBar1.backgroundColor = UIColor.white.cgColor
    view.layer.addSublayer(progressBar1)
    extendBar1()
}

func extendBar1(){
    let transform1 = CATransform3DMakeScale(30, 1, 0)
    let anim = CABasicAnimation(keyPath: "transform")
    anim.isRemovedOnCompletion = false
    anim.fillMode = kCAFillModeForwards
    anim.toValue = NSValue(caTransform3D:transform1)
    anim.duration = 10.00
    progressBar1.add(anim, forKey: "transform")
}

I also tried the following with CATransaction but I get the same result

func extendBar3(){

    let transform1 = CATransform3DMakeScale(30, 1, 0)

    CATransaction.begin()
    CATransaction.setAnimationDuration(7.0)
    progressBar1.transform = transform1
    CATransaction.commit()
}

Solution

  • The chief remaining problem is this line:

        let transform1 = CATransform3DMakeScale(30, 1, 0)
    

    Change the 0 to a 1.

    (The result may still not be the animation you want, precisely, but at least you should see something — as long as (0,600) is not off the screen entirely, of course.)