繁体   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