简体   繁体   English

动画 uibutton 应用图像迅速

[英]animate uibutton applying images swift

I want to apply animation effect by changing two images on button turn by turn.我想通过依次更改按钮上的两个图像来应用动画效果。 I have applied following code but not able to see animation effect.我已经应用了以下代码,但看不到动画效果。 I have applied IBOutlets properly.我已经正确地应用了 IBOutlets。

    var image1:UIImage = UIImage(named: "img_mic_off")!
    var image2:UIImage = UIImage(named: "img_mic_on")!
    btnRecord.imageView?.animationImages = [image1,image1]
    btnRecord.imageView?.animationDuration = 1.0
    btnRecord.imageView!.startAnimating()

Please help me to resolve issue that why it is not working with this given code.请帮助我解决为什么它不能使用此给定代码的问题。 Thanks in advance.提前致谢。

Check out what I've done.看看我做了什么。 The images are changing.图像在变化。 So I hope this is what you actually needed.所以我希望这是你真正需要的。

var image1:UIImage = UIImage(named: "img_mic_off")!
var image2:UIImage = UIImage(named: "img_mic_on")!
btn.setImage(image1, forState: UIControlState.Normal)
btn.imageView!.animationImages = [image1, image2]
btn.imageView!.animationDuration = 1.0
btn.imageView!.startAnimating()

The animation property simply cycles through the image array and changes them at interval of durationInSeconds/numberOfImagesInArray.动画属性只是在图像数组中循环并以 durationInSeconds/numberOfImagesInArray 的间隔更改它们。

So, to have a smooth image animation you will need to have a set of intermediate transition images.因此,要获得流畅的图像动画,您需要有一组中间过渡图像。

Higher the number of images in set, smoother the animation.集合中的图像数量越多,动画就越流畅。

Swift 5斯威夫特 5

Don't forget to unwrap!别忘了拆开! 😊 😊

        if let image1 = UIImage(named: "img_mic_off"), let image2 = UIImage(named: "img_mic_on") {
        btn.setImage(image1, for: UIControl.State.normal)
        btn.imageView?.animationImages = [image1, image2]
        btn.imageView?.animationDuration = 1.0
        btn.imageView?.startAnimating()
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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