以编程方式将多个带有彩色图像的UIButtons添加到UIViewController场景

时间:2016-10-16 03:13:15

标签: ios uiviewcontroller uibutton swift2

我有UIViewController类,我在其中以编程方式向场景添加多个颜色按钮。我有11种不同的颜色,所以我在viewDidLoad函数中添加了11个UIButton。请参阅下文我如何添加灰色和红色按钮。然后我使用函数crayonsPressed动作按标签选择颜色。我这样做是因为UIButton的选择取决于标签值。正如您可以通过UIButton标记值看到这种依赖性,以这种方式添加按钮的方法并不好。我想知道什么是以编程方式将这11个颜色按钮添加到场景的最佳方法,这样我就不必在viewDidLoad中重复11次代码。我的计划是将这个UIViewController子类化为另外11个场景,在这些场景中它们都将具有相同的功能,用于绘制所有11个按钮。

已编辑为使用数组加载所有图像按钮。仍然需要解决如何处理UIButton标签,以便在按下颜色按钮时选择该颜色。

class ViewController: UIViewController {

    let colors: [(CGFloat, CGFloat, CGFloat)] = [
    (0, 0, 0),
    (105.0 / 255.0, 105.0 / 255.0, 105.0 / 255.0),
    (1.0, 0, 0),
    (0, 0, 1.0),
    (51.0 / 255.0, 204.0 / 255.0, 1.0),
    (102.0 / 255.0, 204.0 / 255.0, 0),
    (102.0 / 255.0, 1.0, 0),
    (160.0 / 255.0, 82.0 / 255.0, 45.0 / 255.0),
    (1.0, 102.0 / 255.0, 0),
    (1.0, 1.0, 0),
    (1.0, 1.0, 1.0),
    ]

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let arrayImages : [UIImage] = [UIImage(named: "Grey")!,
                                   UIImage(named: "Red")!,
                                   UIImage(named: "Black")!,
                                   UIImage(named: "Blue")!,
                                   UIImage(named: "Brown")!,
                                   UIImage(named: "DarkGreen")!,
                                   UIImage(named: "DarkOrange")!,
                                   UIImage(named: "Grey")!,
                                   UIImage(named: "LightBlue")!,
                                   UIImage(named: "LightGreen")!,
                                   UIImage(named: "Yellow")!]

    var buttonX : CGFloat = 374.5


    for images in arrayImages {

        let imageButton = UIButton(frame: CGRect(x: buttonX, y: 643, width: 25, height: 125))
        buttonX = buttonX + 25 
        imageButton.setImage(images, for: UIControlState.normal)
        imageButton.addTarget(self, action: #selector(crayonsPressed(_:)), for: .touchUpInside)

        self.view.addSubview(imageButton)

    }

    func crayonsPressed(_ sender: AnyObject) {

    var index = sender.tag ?? 0
    if index < 0 || index >= colors.count {
        index = 0
    }

    (red, green, blue) = colors[index]

    if index == colors.count - 1 {
        opacity = 1.0
    }        
  }
}

2 个答案:

答案 0 :(得分:0)

正如@rmaddy正确建议将颜色放入数组中然后在循环中使用这些颜色。您可以使用变量来跟踪数组的位置。

var i = 0

for images in arrayImages {

    let imageButton = UIButton(frame: CGRect(x: buttonX, y: 643, width: 25, height: 125))
    buttonX = buttonX + 25
    imageButton.tag = i
    imageButton.setImage(images, for: UIControlState.normal)
    imageButton.addTarget(self, action: #selector(self.crayonsPressed(_:)), for: .touchUpInside)

    self.view.addSubview(imageButton)
    i += 1
}

现在,每个按钮都会有一个有效的tag值。

答案 1 :(得分:0)

让Adeel和Rmaddy帮忙后,这是最终的解决方案,可以帮助任何可能需要它的人。 UIButton标记已正确分配给按下的按钮。请注意,这适用于以编程方式向场景添加按钮而不使用Storyboard到ViewController。

    let arrayImages : [UIImage] = [UIImage(named: "Black")!,
                                   UIImage(named: "Grey")!,
                                   UIImage(named: "Red")!,
                                   UIImage(named: "Blue")!,
                                   UIImage(named: "LightBlue")!,
                                   UIImage(named: "DarkGreen")!,
                                   UIImage(named: "LightGreen")!,
                                   UIImage(named: "Brown")!,
                                   UIImage(named: "DarkOrange")!,
                                   UIImage(named: "Yellow")!,
                                   UIImage(named: "Eraser")!]

    var buttonX : CGFloat = 374.5
    var i = 0        
    for images in arrayImages {

       let imageButton = UIButton(frame: CGRect(x: buttonX, y: 643, width: 25, height: 125))
        buttonX = buttonX + 25 // space buttons 25px apart
        imageButton.setImage(images, for: UIControlState.normal)
        imageButton.tag = i            
        imageButton.addTarget(self, action: #selector(crayonsPressed(_:)), for: .touchUpInside)

        self.view.addSubview(imageButton)
        i += 1
    }

    func crayonsPressed(_ sender: AnyObject) {      
    var index = sender.tag ?? 0
    if index < 0 || index >= colors.count {
        index = 0
    }
    (red, green, blue) = colors[index]

    if index == colors.count - 1 {
        opacity = 1.0
    }        
    }