0

タイトルがすべてを物語っています。これが私のコードです:

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)
}
4

1 に答える 1