How to know when the button is pressed and cancele

2019-07-24 05:52发布

I made a button. And used the below code. But it doesn't work at all.

override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
    print("pressTest")
    btnConvert.isHidden = true
}

How can I know when the button is pressed and when button pressing is canceled?

1条回答
forever°为你锁心
2楼-- · 2019-07-24 06:33

Use the built-in UIButton functions - .touchDown, .touchUpInside, .touchUpOutside.

Touch begin:

myButton.addTarget(target, action: action, for: .touchDown)

Touch ends:

myButton.addTarget(target, action: action, for: .touchUpInside)
myButton.addTarget(target, action: action, for: .touchUpOutside)
查看更多
登录 后发表回答