简体   繁体   中英

Subclassed CALayer animation function does not animate

Currently, I have subclassed CALayer to create a animatable bottom border for my UITextField. However, my animateColor function is not working. I've tested to see if the function is being called and it is. However, no animation is taking place. What am I doing wrong?

class KTextFieldBottomBorder:CALayer {

override init!() {
    super.init()
    commonInit()
}

override init!(layer: AnyObject!) {
    super.init(layer: layer)
    commonInit()
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

func commonInit() {

    // Appearance

    self.backgroundColor = UIColor.lightGrayColor().CGColor

}

// MARK: Animations

func animateColor (endingColor:UIColor, duration:CFTimeInterval) {
    var colorAnimation = CABasicAnimation(keyPath: "color")
    colorAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    colorAnimation.toValue = endingColor.CGColor
    colorAnimation.duration = duration
    colorAnimation.fillMode = kCAFillModeForwards
    super.addAnimation(colorAnimation, forKey: "color")
    println("X")
}

}

You need to define the property that shall be animated by setting the keyPath .

colorAnimation.keyPath = "backgroundColor"

The key argument of addAnimation is merely used as an identifier for the animation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM