将UIButton图像从一个图像转换为另一个图像

时间:2021-05-28 21:21:53

I'm new to iOS development and am using Swift as my language of choice. I'm developing a simple little program for my son which displays images of animals on UIButtons and when he picks the right one all the buttons load new images and he's asked to find a different animal.

我是iOS开发的新手,我使用Swift作为我的首选语言。我正在为我的儿子开发一个简单的小程序,在UIButtons上显示动物的图像,当他选择正确的按钮时,所有按钮都会加载新图像,并且他被要求找到不同的动物。

The basics of the program are up and running and he can play it but now I want to get on to making it fancier. When the correct image is selected and it's time to replace the button images with new ones I use:

该程序的基础知识已经启动并运行,他可以播放它,但现在我想继续让它变得更加漂亮。当选择了正确的图像并且是时候用新的按钮图像替换按钮图像了:

btnItem.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)

Rather than simply changing a button's image from one to another I want to do the change with an animated transition but I can't figure out how.

我不想简单地将按钮的图像从一个图像更改为另一个图像,而是希望通过动画过渡进行更改,但我无法弄清楚如何。

I've found plenty to do with transitions on UIViews and plenty to do with permanently animating button images but nothing for transitions such as a dissolve or a swipe from left to right or whatever.

我发现很多关于UIViews的过渡和很多与永久动画按钮图像有关,但没有任何过渡,如从左到右或任何其他过程中的溶解或滑动。

This question on SO is pretty much the same as mine but the answer uses Objective-C.

关于SO的这个问题与我的几乎相同,但答案使用了Objective-C。

So, long story short, is there a way of transitioning a UIButton image from one to another in Swift?

所以,长话短说,有没有办法在Swift中将UIButton图像从一个转换到另一个?

Thanks for your time in advance.

感谢您提前的时间。

1 个解决方案

#1


21  

Updating below with Swift 4.0

使用Swift 4.0更新以下内容

 UIView.transition(with: sender as! UIView, duration: 1.5, options: .transitionFlipFromRight, animations: {
            sender.setImage(UIImage(named: arrItems[intItemCounter]), for: .normal)
        }, completion: nil)

UIButton is a subclass of UIView so you can use UIView.animateWithDuration with a button. For example you can say something like:

UIButton是UIView的子类,因此您可以将UIView.animateWithDuration与按钮一起使用。例如,您可以这样说:

@IBAction func buttonPressed(sender: UIButton) {
        UIView.transitionWithView(sender, duration: 1.5, options: .TransitionFlipFromRight, animations: {
                sender.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
            }, completion: nil)

    }

Which will cause, on tapping, the button to flip with the new image.

在点击时,这将导致按钮翻转新图像。

If you want the animation from the other example (fade out/in) you can say:

如果你想要其他例子中的动画(淡出/淡出),你可以说:

@IBAction func buttonPressed(sender: UIButton) {
            UIView.animateWithDuration(0.5, animations: {
                    sender.alpha = 0.0
                }, completion:{(finished) in
                    sender.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
                    UIView.animateWithDuration(0.5,animations:{
                    sender.alpha = 1.0
                    },completion:nil)
             })

        }

You can also do a cross dissolve like so:

您也可以像这样进行交叉溶解:

@IBAction func buttonPressed(sender: UIButton) {
        UIView.transitionWithView(sender, duration: 1.5, options: .TransitionCrossDissolve, animations: {
            sender.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
            }, completion: nil)

    }

#1


21  

Updating below with Swift 4.0

使用Swift 4.0更新以下内容

 UIView.transition(with: sender as! UIView, duration: 1.5, options: .transitionFlipFromRight, animations: {
            sender.setImage(UIImage(named: arrItems[intItemCounter]), for: .normal)
        }, completion: nil)

UIButton is a subclass of UIView so you can use UIView.animateWithDuration with a button. For example you can say something like:

UIButton是UIView的子类,因此您可以将UIView.animateWithDuration与按钮一起使用。例如,您可以这样说:

@IBAction func buttonPressed(sender: UIButton) {
        UIView.transitionWithView(sender, duration: 1.5, options: .TransitionFlipFromRight, animations: {
                sender.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
            }, completion: nil)

    }

Which will cause, on tapping, the button to flip with the new image.

在点击时,这将导致按钮翻转新图像。

If you want the animation from the other example (fade out/in) you can say:

如果你想要其他例子中的动画(淡出/淡出),你可以说:

@IBAction func buttonPressed(sender: UIButton) {
            UIView.animateWithDuration(0.5, animations: {
                    sender.alpha = 0.0
                }, completion:{(finished) in
                    sender.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
                    UIView.animateWithDuration(0.5,animations:{
                    sender.alpha = 1.0
                    },completion:nil)
             })

        }

You can also do a cross dissolve like so:

您也可以像这样进行交叉溶解:

@IBAction func buttonPressed(sender: UIButton) {
        UIView.transitionWithView(sender, duration: 1.5, options: .TransitionCrossDissolve, animations: {
            sender.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
            }, completion: nil)

    }