簡體   English   中英

單擊Swift 3 iOS時UIButton無動作

[英]UIButton no action when click swift 3 iOS

我的自定義UIButton有問題。 添加目標時,單擊時不執行任何操作。 我試圖將函數移到ViewController類中,徒然更改選擇器名稱和類型。 有我的代碼。 希望您能夠幫助我。 (層次結構)CustomButton→MenuButtonGroup→ViewController

import UIKit

class CustomButton: UIButton {

/** vue 1 */
private let view1 = UIImageView()

/** vue 2 */
private let view2 = UIView()

/** couleur du bouton */
private var color = ""

/** décalage y pour le 3d */
private let relief = screenHeight*0.013


init(frame: CGRect, color: String){
    super.init(frame: frame)


    self.color = color
    self.layer.cornerRadius = 10

    //définir la couleur de l'ombre
    switch color {
    case "red":
        self.backgroundColor = shadowRedColor
    case "green":
        self.backgroundColor = shadowGreenColor
    case "blue":
        self.backgroundColor = shadowBlueColor
    case "yellow":
        self.backgroundColor = shadowYellowColor
    default:
        self.backgroundColor = shadowRedColor
    }

    mainColorView()

self.addSubview(view2)

    self.addTarget(self, action: #selector(downAction), for: .touchDown)
    self.addTarget(self, action: #selector(touchUp), for: .touchUpInside)
    self.addTarget(self, action: #selector(touchUp), for: .touchUpOutside)

}

/** vue de couleur principale*/
func mainColorView(){

    view2.frame = CGRect(x: 0, y: -relief, width: self.frame.width, height: self.frame.height)
    view2.layer.cornerRadius = 10

    //définir la couleur de l'ombre
    switch color {
    case "red":
        view2.backgroundColor = redColor
    case "green":
        view2.backgroundColor = greenColor
    case "blue":
        view2.backgroundColor = blueColor
    case "yellow":
        view2.backgroundColor = yellowColor
    default:
        view2.backgroundColor = redColor
    }
}

/** appuis sur le bouton */
func downAction(sender: UIButton){
    print("prout")
    UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseOut, animations: {
        self.view2.transform = CGAffineTransform(translationX: 0, y: self.relief)
    })
}

/**relacher le bouton*/
func touchUp(){
    animUp()
}

/** animer le relachement du bouton*/
func animUp(){
    UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.3, options: .curveEaseInOut, animations: {
        self.view2.transform = CGAffineTransform.identity
    })
}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

將按鈕添加到MenuButtonGroup:

playButton = CustomButton(frame: CGRect(x: 0, y: 0, width: 300, height: 300), color: "green")

該按鈕顯示在屏幕上,但根本沒有任何動作。 我嘗試了BringSubviewToFront,但沒有任何效果。

在將所謂的view2添加到UIButton ,您將其設置為與按鈕的大小相同,如下所示:

view2.frame = CGRect(x: 0, y: -relief, width: self.frame.width, height: self.frame.height)

這意味着view2覆蓋了下面的整個按鈕,並且吞沒了您按鈕的觸摸事件。 請執行下列操作:

view2.isUserInteractionEnabled = false

生活將永遠美好。

編輯:

查看完項目后,很明顯,您正在將此按鈕作為子視圖添加到UIImageView中。 問題仍然是相同的,默認情況下, UIImageView isUserInteractionEnabled設置為false

因此,請確保在MenuButtonGroup的初始化程序中將其設置為true

self.isUserInteractionEnabled = true

因此,請禁用UIView實例的用戶交互,並啟用UIImageView子類的用戶交互。

生活會更好。

您分配的操作不正確。

您的動作聲明是func downAction(sender: UIButton) ,但是您要添加func downAction() 您需要像這樣添加它:

self.addTarget(self, action: #selector(downAction(sender:)), for: .touchDown)

嘗試更改此:

init(frame: CGRect, color: String) {
    super.init(frame: frame)

    self.color = color
    self.layer.cornerRadius = 10

    //définir la couleur de l'ombre
    switch color {
    case "red":
        self.backgroundColor = shadowRedColor
    case "green":
        self.backgroundColor = shadowGreenColor
    case "blue":
        self.backgroundColor = shadowBlueColor
    case "yellow":
        self.backgroundColor = shadowYellowColor
    default:
        self.backgroundColor = shadowRedColor
    }    

    mainColorView()

    self.addSubview(view2)

    self.addTarget(self, action: #selector(downAction), for: .touchDown)
    self.addTarget(self, action: #selector(touchUp), for: .touchUpInside)
    self.addTarget(self, action: #selector(touchUp), for: .touchUpOutside)
}

對此:

init(frame: CGRect, color: String) {
    super.init(frame: frame)

    self.color = color
    self.layer.cornerRadius = 10

    //définir la couleur de l'ombre
    switch color {
        case "red":
             self.backgroundColor = shadowRedColor
        case "green":
             self.backgroundColor = shadowGreenColor
        case "blue":
             self.backgroundColor = shadowBlueColor
        case "yellow":
             self.backgroundColor = shadowYellowColor
        default:
             self.backgroundColor = shadowRedColor
    }

    mainColorView()

    self.view2.addTarget(self, action: #selector(CustomButton.downAction), for: .touchDown)
    self.view2.addTarget(self, action: #selector(CustomButton.touchUp), for: .touchUpInside)
    self.view2.addTarget(self, action: #selector(CustomButton.touchUp), for: .touchUpOutside)

    self.addSubview(view2)
}

編輯:嘗試從功能定義中刪除發件人:UIButton:

func downAction() {
    print("prout")
    UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseOut, animations: {
        self.view2.transform = CGAffineTransform(translationX: 0, y: self.relief)
    })
}

EDIT2:嘗試將view2類型從UIView()更改為UIButton()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM