gpt4 book ai didi

ios - Swift 图像按钮上的淡入淡出(过渡效果)

转载 作者:可可西里 更新时间:2023-11-01 01:39:57 40 4
gpt4 key购买 nike

我有一个带有默认状态和高亮状态的 UIButton,所以当它处于默认状态时它显示 image.png,当被点击时它显示 imageOn.png

这与图像之间唯一的变化是背景颜色。

我无法做的事情是在单击按钮时在这两个图像之间进行淡入淡出,以进行精美的背景颜色更改。我正在使用 Swift。

最佳答案

好吧,我现在没有时间测试这个,但我认为你的代码应该看起来像这样。

override func viewDidLoad() {

let normalImage = UIImage(named: "image.png") as UIImage!
let selectedImage = UIImage(named: "imageOn.png") as UIImage!

var mybutton:UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton

mybutton.setTitle("Your title", forState: UIControlState.Normal)
mybutton.frame = CGRectMake(0, 0, 100, 44)

self.view.addSubview(button as UIView)
//set normal image
mybutton.setImage(normalImage, forState: UIControlState.Normal)
//set highlighted image
mybutton.setImage(selectedImage, forState: UIControlState.Selected)

mybutton.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

}

func buttonClicked(sender:UIButton)
{

if (sender.selected) {
UIView.animateWithDuration(1.5, animations: {
self.normalImage.alpha = 1.0
self.selectedImage.alpha = 0.0
sender.selected = !sender.selected;
}
else{
UIView.animateWithDuration(1.5, animations: {
self.normalImage.alpha = 0.0
self.selectedImage.alpha = 1.0
}

}

祝你好运!

关于ios - Swift 图像按钮上的淡入淡出(过渡效果),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30786258/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com