UIButton 无法以编程方式在 UIKit 中工作

问题描述 投票:0回答:1

我一直在 swift UIKit 中以编程方式构建一个绘图应用程序。我已经能够为我在

UIButtons
枚举中定义的每种情况创建
Pencil
。我可以看到每个
Pencil.color
都出现了每个按钮。我的问题是,我创建了一个名为
@obj
buttonClicked
函数,该函数应该分配与每个按钮对应的颜色,但是当我单击它时,它不起作用,甚至无法在检测到单击事件的控制台中打印。

这是保存我的铅笔画笔属性的变量:

 var lastPoint = CGPoint.zero
    var color = UIColor.black
    var brushWidth: CGFloat = 10.0
    var opacity: CGFloat = 1.0
    var swiped = false

这是我分配每种颜色的

buttonClicked
函数:

  @objc func buttonClicked(_ sender: UIButton){
        guard let pencil = Pencil(tag: sender.tag) else {
          return
        }
        
        color = pencil.color
     
        if pencil == .eraser {
          opacity = 1.0
        }
        print("button Clicked")
    }

为每个

Pencil.color

创建按钮的函数
    func createColorButton() -> [UIButton]{
        
        var buttons: [UIButton] = []
        for pencilCase in Pencil.allCases {
            let button = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
            button.backgroundColor = pencilCase.color
            button.tag = pencilCase.hashValue
            button.addTarget(self, action: #selector(self.buttonClicked(_:)), for: .touchUpInside)

            buttons.append(button)
        }
     return buttons
    }

为了提供上下文,这里引用了我的

Pencil 
enum

enum Pencil: CaseIterable {
    
    case black
    case grey
    case red
    case darkblue
    case lightBlue
    case darkGreen
    case lightGreen
    case brown
    case orange
    case yellow
    case eraser
    
    init?(tag: Int) {
        switch tag {
        case 1:
          self = .black
        case 2:
          self = .grey
        case 3:
          self = .red
        case 4:
          self = .darkblue
        case 5:
          self = .lightBlue
        case 6:
          self = .darkGreen
        case 7:
          self = .lightGreen
        case 8:
          self = .brown
        case 9:
          self = .orange
        case 10:
          self = .yellow
        case 11:
          self = .eraser
        default:
          return nil
        }
      }
    // cases for penil.color 
    var color: UIColor {
      switch self {
      case .black:
        return .black
      case .grey:
        return UIColor(white: 105/255.0, alpha: 1.0)
      case .red:
        return UIColor(red: 1, green: 0, blue: 0, alpha: 1.0)
      case .darkblue:
        return UIColor(red: 0, green: 0, blue: 1, alpha: 1.0)
      case .lightBlue:
        return UIColor(red: 51/255.0, green: 204/255.0, blue: 1, alpha: 1.0)
      case .darkGreen:
        return UIColor(red: 102/255.0, green: 204/255.0, blue: 0, alpha: 1.0)
      case .lightGreen:
        return UIColor(red: 102/255.0, green: 1, blue: 0, alpha: 1.0)
      case .brown:
        return UIColor(red: 160/255.0, green: 82/255.0, blue: 45/255.0, alpha: 1.0)
      case .orange:
        return UIColor(red: 1, green: 102/255.0, blue: 0, alpha: 1.0)
      case .yellow:
        return UIColor(red: 1, green: 1, blue: 0, alpha: 1.0)
      case .eraser:
        return .white
      }
    }
}

我尝试在

viewDidLoad
中调用addTarget,但这也不起作用,所以这是我的代码的第一个版本

override func viewDidLoad() {
        super.viewDidLoad()
        
      
        let buttons = createColorButton()
        stackView.spacing = 5
        for button in buttons {
            stackView.addArrangedSubview(button)
                       button.layer.borderWidth = 1
                       button.layer.borderColor = UIColor.blue.cgColor
            
                       button.layer.cornerRadius = 10
                
            }
        view.addSubview(stackView)
        NSLayoutConstraint.activate([
            stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200)
            ])
        
        setupImageViews()
        
    }

我还可以尝试做些什么来使

createColorButton
发挥作用?

swift uikit uibutton addtarget programmatically
1个回答
0
投票

有多种方法可以使用枚举......对于您的情况,很容易使用

Pencil.allCases.enumerated()
并将按钮标签设置为索引:

class ColorButtonsVC: UIViewController {
    
    var color: UIColor = .white
    var opacity: CGFloat = 1.0

    let cLabel = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let buttons = createColorButton()
        let stackView = UIStackView()
        stackView.spacing = 5
        for button in buttons {
            stackView.addArrangedSubview(button)
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor.blue.cgColor
            
            button.layer.cornerRadius = 10
            
        }
        stackView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stackView)
        NSLayoutConstraint.activate([
            stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200),
            stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        ])
        
        cLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(cLabel)
        NSLayoutConstraint.activate([
            cLabel.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 8.0),
            cLabel.widthAnchor.constraint(equalToConstant: 200.0),
            cLabel.heightAnchor.constraint(equalToConstant: 60.0),
            cLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        ])
        cLabel.text = "Color"
        cLabel.textAlignment = .center
        //setupImageViews()
        
    }
    
    func createColorButton() -> [UIButton]{
        var buttons: [UIButton] = []
        for (idx, pencilCase) in Pencil.allCases.enumerated() {
            let button = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
            button.backgroundColor = pencilCase.color
            // the Pencil enum starts at 1 instead of Zero
            //  so add 1 to the index
            button.tag = idx + 1
            button.addTarget(self, action: #selector(self.buttonClicked(_:)), for: .touchUpInside)
            buttons.append(button)
        }
        return buttons
    }
    
    @objc func buttonClicked(_ sender: UIButton){

        guard let pencil = Pencil(tag: sender.tag) else {
            return
        }
        
        color = pencil.color
        
        if pencil == .eraser {
            opacity = 1.0
        }
        print("button Clicked:", sender.tag)
        
        cLabel.backgroundColor = color
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.