How do I animate opacity using swift?

后端 未结 4 1911
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 02:00

Could someone please provide me with an example of animating the opacity of an Image View in swift??

I couldn\'t even find a good example in objective c



        
4条回答
  •  执念已碎
    2021-01-18 02:29

    You can use this extension method (unless you want to modify the view's attributes itself):

    extension CALayer {
    
        func flash(duration: TimeInterval) -> Observable {
            let flash = CABasicAnimation(keyPath: "opacity")
    
            flash.fromValue = NSNumber(value: 0)
            flash.toValue = NSNumber(value: 1)
            flash.duration = duration
            flash.autoreverses = true
    
            removeAnimation(forKey: "flashAnimation")
            add(flash, forKey: "flashAnimation")
            opacity = 0     // Change the actual data value in the layer to the final value
        }
    }
    

提交回复
热议问题