单击iOS滚动视图中的按钮添加边框,并从其他按钮中删除边框

时间:2018-04-22 08:15:19

标签: ios uiscrollview uibutton swift4

我在iOS中向水平滚动视图添加了按钮。

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

     func setUpScrollView() {
        let buttonPadding:CGFloat = 10
        var xOffset:CGFloat = 10

        for i in 0 ... 10 {
            let button = UIButton()
            button.tag = i
            button.backgroundColor = UIColor.red
            button.setTitle("\(i)", for: .normal)

            if(button.tag==currentTag){
           button.addTarget(self, action: #selector(btnTouchUnselect), for: UIControlEvents.touchUpInside)
            }
            else{

                     button.addTarget(self, action: #selector(btnTouch), for: UIControlEvents.touchUpInside)
            }
            button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 70, height: 30)

            xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width;
            scrollView.addSubview(button)
        }

        scrollView.contentSize = CGSize(width: xOffset, height: scrollView.frame.height)
    }

    @objc func btnTouch(button:UIButton){
        print("tap touch",button.tag)
            button.layer.borderColor = UIColor.black.cgColor
            button.layer.borderWidth = 1.0
        currentTag = button.tag

    }

    @objc func  btnTouchUnselect(button:UIButton){
        button.layer.borderColor = UIColor.white.cgColor
        button.layer.borderWidth = 1.0

    }

}

我希望按钮在用户点击它时获得不同的边框颜色,而其他按钮保持黑色。但是,当我使用此代码时,它会将所有单击的按钮边框变为黑色,并且不会将单击的按钮边框变为白色。

目的示例: - 假设我有10个按钮,我想要点击按钮1,然后它的边框变白,其他'保持黑色;如果单击按钮2,则所有边框都会再次变黑,包括按钮1,只有按钮2的边框变为白色。

我需要一些指导来实现这一目标。

2 个答案:

答案 0 :(得分:0)

试试这段代码

var allButtons = [UIButton]() 

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

 func setUpScrollView() {
    let buttonPadding:CGFloat = 10
    var xOffset:CGFloat = 10

    for i in 0 ... 10 {
        let button = UIButton()
        button.tag = i
        button.backgroundColor = UIColor.red
        button.layer.borderWidth = 1.0
        button.setTitle("\(i)", for: .normal)
        button.addTarget(self, action: #selector(didTap), for: UIControlEvents.touchUpInside)
        button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 70, height: 30)

        xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width;
        scrollView.addSubview(button)
        allButtons.append(button)
    }

    scrollView.contentSize = CGSize(width: xOffset, height: scrollView.frame.height)
}

@objc func didTap(button: UIButton) {
    print("tap touch",button.tag)

    allButtons.forEach { inButton in 
         if inButton == button {
            button.layer.borderColor = UIColor.black.cgColor
         } else {
            button.layer.borderColor = UIColor.white.cgColor
         }
    }
    currentTag = button.tag
}

答案 1 :(得分:0)

我认为问题是您的按钮只能在setScrollView中访问。 所以当按下一个按钮时,在@Anton回答中,只需点击按钮就可以在didTap功能中识别。

我认为更好的想法是制作一个UIButtons的数组, 在setScrollView中启动它们, 然后使用@Anton didTap函数

class yourClass {

var buttons : [UIButton] = Array(repeatElement(UIButton(), count: 10))

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

func setUpScrollView() {
        let buttonPadding:CGFloat = 10
        var xOffset:CGFloat = 10

        for i in 0...9 {
            buttons[i].tag = i
            buttons[i].backgroundColor = UIColor.red
            buttons[i].setTitle("\(i)", for: .normal)

         //Other functionality that you had set here before...
    }

@objc func didTap(clickedButton: UIButton) {

    for eachButton in self.buttons { 
         if eachButton.tag == clickedButton.tag {
            eachButton.layer.borderColor = UIColor.white.cgColor
         } else {
            eachButton.layer.borderColor = UIColor.balck.cgColor
         }
    }
    currentTag = clickedButton.tag
}


}