【问题标题】:UIButton text not visible when animating button image为按钮图像设置动画时,UIButton 文本不可见
【发布时间】:2018-03-09 19:50:14
【问题描述】:

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

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()   
}

怎么了?

【问题讨论】:

  • 你在选项数组中添加了什么?
  • @ishika 这些是 UIButtons。
  • 我的意思是问您是否以编程方式创建了插座或按钮?
  • @ishika 我创建了带有插座的 UIButton。并且有6个这样的按钮。我想在按钮上切换图像以突出显示和取消突出显示 3 个按钮。哪些被添加到数组中(选项)
  • 你真的不需要管理所有这些数组和所有......你可以通过故事板管理所有这些状态,只需要根据需要更改其状态。其余的一切都将按照状态配置中的设置显示。

标签: ios swift swift3 uibutton uilabel


【解决方案1】:

我会尽量给你一个简单的解决方案。 取一个按钮并从情节提要本身调整其选定状态和默认状态。例如,选择一个按钮的默认状态,并从 IBInspector 给它一个标题和图像。然后对所选状态重复相同的过程以及选择状态配置。

然后你也可以管理按钮的图片和标题如图。

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

       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
    }
}

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

【讨论】:

  • 当我从另一个视图进入视图时。我希望视图启动按钮,如闪烁的灯泡。 (切换 2 张图片。)上述解决方案可以吗?
  • 我希望切换循环继续,直到有点击。在任何选项上。
  • 哦,这就像一个魅力。我已经进行了必要的更改以满足我的要求。
  • 嘿..抱歉耽搁了..您可以使用计时器和切换代码中的更新..此外,可以在单击按钮时停止
  • 这正是我所需要的。感谢您的精彩更新。这解决了一个很大的时间问题。自 2 天以来一直在寻找解决方案。
【解决方案2】:

您可以将 UIButton 的背景图像设置为正常状态和选中状态,然后在状态之间切换时设置动画(在点击按钮时切换选中和未选中)

【讨论】:

  • 但我想不出任何方法来为背景图像设置动画。你能介意吗。为其提供和编码框架。提前致谢。
【解决方案3】:

您是否尝试将图片设置为背景图片?

option?.setBackgroundImage( UIImage(named: "green_button.png")!, for: .normal)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2013-06-06
    • 2015-08-17
    相关资源
    最近更新 更多