8

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

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

4

2 回答 2

16

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

self.addTarget(self, action: "pressed", forControlEvents: [.touchDown])
self.addTarget(self, action: "released", forControlEvents: [.touchDragExit, .touchUpInside, .touchUpOutside, .touchCancel])
于 2016-01-16T14:37:19.513 回答
1

获得高亮状态:UIControlEventTouchDown

失去高亮状态:UIControlEventTouchDragOutside

于 2016-01-12T15:46:12.513 回答