gpt4 book ai didi

ios - 动画按钮图像时 UIButton 文本不可见

转载 作者:搜寻专家 更新时间:2023-11-01 06:31:41 25 4
gpt4 key购买 nike

我必须在 UIButton 上切换两个图像(带动画),但这会将我的标签隐藏在 UIButton 上的图像后面。因此,我向其中添加了 UILabel 的 subview ,但也没有显示。我什至尝试将 subview 置于最前面,但这也不起作用。

let options = [option_1, option_2, option_6]
var imageArray = [UIImage]()

imageArray.append( UIImage(named: "light_button.png" )! )
imageArray.append( UIImage(named: "dark_button.png" )! )

let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.option_1.frame.width, height: self.option_1.frame.width))
label.text = "HEY"
label.font = UIFont.systemFont(ofSize: 18)
option_1.addSubview(label)
option_1.bringSubview(toFront: label)

for option in options{
option?.setImage( UIImage(named: "green_button.png")!, for: .normal)

option?.imageView!.animationImages = imageArray
option?.imageView!.animationImages = imageArray

option?.setTitle("TEST TEXT", for: .normal) //Even this don't work

option?.imageView!.animationDuration = 2.35
option?.imageView!.startAnimating()
}

怎么了?

最佳答案

我将尝试为您提供一个简单的解决方案。拿一个按钮并从 Storyboard本身调整其选择和默认状态。例如,选择一个按钮的默认状态,并从 IBInspector 给它一个标题和图像。然后对所选状态重复相同的过程以及选择状态配置。 enter image description here

然后您还可以管理按钮的图像和标题,如图所示。 enter image description here

最后你可以管理你的代码如下:

       class ViewController: UIViewController {
@IBOutlet weak var buttonObj: UIButton!
var stop = false
var timer:Timer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// toggleButtons()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.toggleButtons), userInfo: nil, repeats: true);
}

func toggleButtons(){
if stop{
timer?.invalidate()
}else{
buttonObj.toggleSelection()
}


}

@IBAction func btnClicked(_ sender: Any) {

//Once you click the button stop will become true and hence your timer will invalidate
stop = true
}
}
//MARK:-******** UIButtonExtension ***********
extension UIButton {
func toggleSelection() {
self.selected = self.selected ? false : true
}
}

您还可以根据需要在各种动画类型之间进行选择。

关于ios - 动画按钮图像时 UIButton 文本不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46462864/

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