UIImage animationImages tint color?

前端 未结 6 1793
醉话见心
醉话见心 2020-12-31 13:58

Is there a way to tint the images in an animation?

I know I can tint a single image like this:

var imageOne:UIImage = UIImage(named: \"pullto_1.png\"         


        
6条回答
  •  忘掉有多难
    2020-12-31 14:21

    Maybe this example extension helps:

     extension UIImageView {
    
            func pulsingTintColor() {
                UIView.animate(withDuration: 2, delay: 0.0, options: [.repeat, .autoreverse], animations: {
                             self.tintColor = UIColor.red
                             self.tintColor = UIColor.green
                             self.tintColor = UIColor.blue
                }, completion: nil)
            }
        }
    

    Ensure you have set the Render as: Template Image option in your asset catalog. This works for UIViews as well. Just replace tintColor with backgroundColor.

    If you need parametrised colours:

    func pulsingTintColor(with colors: [UIColor] = [UIColor.red, UIColor.green, UIColor.blue]) {
        UIView.animate(withDuration: 2, delay: 0.0, options: [.repeat, .autoreverse], animations: {
             colors.forEach({self.tintColor = $0})
        }, completion: nil)
    }
    

提交回复
热议问题