【问题标题】:3 buttons change between actions and images3 个按钮在动作和图像之间切换
【发布时间】:2016-09-02 11:53:12
【问题描述】:

我有 3 个按钮,它们都有图像和动作。但是,当按下 centerButton 时,它们都应该改变动作和图像。最简单的方法是什么?

@IBOutlet weak var leftButton: UIButton!
@IBOutlet weak var rightButton: UIButton!
@IBOutlet weak var centerButton: UIButton!


override func draw(_ rect: CGRect) {
    // Drawing code

    centerButton.backgroundColor = UIColor(rgba: "#4834B4")
    centerButton.layer.cornerRadius = self.centerButton.frame.width / 2
    centerButton.addTarget(self, action: #selector(centerButtonClicked), for: UIControlEvents.touchUpInside)

    leftButton.addTarget(self, action: #selector(rightButtonClicked), for: UIControlEvents.touchUpInside)

    rightButton.addTarget(self, action: #selector(rightButtonClicked), for: UIControlEvents.touchUpInside)




}

func centerButtonClicked() {
    if !pictureTaken {
        //change action
    } else {
        //change action
    }



}

【问题讨论】:

  • 告诉我们你尝试了什么
  • 动作意味着..?
  • 我已经添加了一些我制作的代码,但是我想知道是否有更聪明的东西?这是在 UIView 子类中
  • 这里的action是什么意思?
  • 我的意思是按下按钮时要调用什么函数/要运行什么代码

标签: ios swift uiimageview uibutton uiimage


【解决方案1】:

如果我很清楚你想要这样的东西:

func leftButtonClicked() {
    if centerButton.backgroundColor == UIColor.yellowColor() {
        centerButton.setBackgroundImage(image, forState: .Normal)
    } else {
        rightButton.setBackgroundImage(image, forState: .Normal)
    }

}




func centerButtonClicked() {
   if leftButton.backgroundColor == UIColor.yellowColor() {
        leftButton.setBackgroundImage(image, forState: .Normal)
    } else {
        rightButton.setBackgroundImage(image, forState: .Normal)
    }

我写了两个带有图像变化的动作。

【讨论】:

    猜你喜欢
    • 2011-10-08
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多