什么控制事件开始和结束突出显示UIButton的状态

Kir*_*ins 7 uibutton ios uicontrolevents swift

我正在用UIButton钢琴键创建类似钢琴的视图.UIControlEvents当按钮获取并失去突出显示状态时,我应该听什么才能获得回调?

我试图创建子类UIButton并添加属性观察器highlighted,它工作正常.但是有时候我需要从代码中手动设置突出显示的状态,因为没有办法判断事件是用户还是应用程序启动,所以真的搞砸了.

Kir*_*ins 11

为了模仿钢琴键的行为,我使用了以下内容UIControlEvents:

self.addTarget(self, action: "pressed", forControlEvents: [.touchDown])
self.addTarget(self, action: "released", forControlEvents: [.touchDragExit, .touchUpInside, .touchUpOutside, .touchCancel])
Run Code Online (Sandbox Code Playgroud)

  • 您可能还希望“touchDragEnter”触发“pressed” (2认同)