标题说明了一切。这是我的代码:
func createCheckBoxButton(xPos: CGFloat, yPos: CGFloat, tag: Int) -> UIButton {
var checkBox = UIButton(frame: CGRect(x: xPos, y: yPos, width: checkBoxSize, height: checkBoxSize))
checkBox.setBackgroundImage(UIImage(named: "checkbox_inactive"), forState: UIControlState.Normal)
checkBox.setBackgroundImage(UIImage(named: "checkbox_pressed"), forState: UIControlState.Highlighted)
checkBox.setBackgroundImage(UIImage(named: "checkbox_active"), forState: UIControlState.Selected)
checkBox.tag = tag
checkBox.contentMode = .ScaleAspectFit
checkBox.addTarget(self, action: "processButton:", forControlEvents: UIControlEvents.TouchUpInside)
return checkBox
}
当我的按钮被按下时,有一个被调用的函数:
func processButton(sender: UIButton) {
if (answerViewArray[sender.tag].backgroundColor == UIColor.whiteColor()) {
answerViewArray[sender.tag].backgroundColor = myColor.pinky()
} else {
answerViewArray[sender.tag].backgroundColor = UIColor.whiteColor()
}
let tag = answerButtonsArray[sender.tag]
answer.buttonPressed(tag)
}
当我启动应用程序时,checkbox_inactive
图像就在那里。当我按下并按住它时,checkbox_pressed
图像就会出现。但是当我释放我的点击时,会checkbox_inactive
再次出现而不是checkbox_active
.
我也尝试了一个UIImageView
,这实际上对我来说是最好的解决方案。我将我的复选框设置为一个UIImageView
,并在我的一般视图的顶部放置一个不可见的视图,以便我可以在任何地方单击。但是当我按下我的隐形视图时,UIImageView
它就消失了。
这是代码:
func createCheckBoxButton(xPos: CGFloat, yPos: CGFloat) -> UIImageView {
var checkBox = UIImageView(frame: CGRect(x: xPos, y: yPos, width: checkBoxSize, height: checkBoxSize))
checkBox.image = UIImage(named: "checkbox_inactive")
checkBox.contentMode = .ScaleAspectFit
return checkBox
}
这是调用的函数:
func processButton(sender: UIButton) {
if (answerViewArray[sender.tag].backgroundColor == UIColor.whiteColor()) {
answerViewArray[sender.tag].backgroundColor = myColor.pinky()
checkBoxArray[sender.tag].image = UIImage(named: "checkbox-active")
} else {
answerViewArray[sender.tag].backgroundColor = UIColor.whiteColor()
checkBoxArray[sender.tag].image = UIImage(named: "checkbox-inactive")
}
let tag = answerButtonsArray[sender.tag]
answer.buttonPressed(tag)
}