【发布时间】:2018-10-02 11:37:32
【问题描述】:
我在 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 的边框会变为白色。
我需要一些指导来实现这一目标。
【问题讨论】:
-
我想你想当一个按钮被点击时其余的按钮变成白色,对吗?
标签: ios uiscrollview uibutton swift4