Slide to and from UIButtons and make them respond

2019-09-02 05:45发布

问题:

I am creating a semi-piano app in Objective-C for iPhone; it's in fact a diffrent layout MIDI Controller, but I'm still working on the design.

I created all of the "keys" with UIButtons, and I want to be able to slide to them or from them to other UIButtons.

I read all the similar questions before, but I couldn't make it to work with touchesMoved, Because it only worked from the view if I didn't drag the finger from a UIButton.

Thanks in advance, code will be appreciated!

回答1:

Are your buttons created in IB? The default event when connecting buttons in IB is "touch up inside", which means the action is fired when the user ends a touch (lifts the finger) inside the button. A piano key or similar would be more appropriate to fire on the "touch down inside" (for the initial press) and the "touch drag enter" (for when a finger that is already on the screen moves into the button).

Since you are doing it programmatically, you can add targets to the buttons as described here:

http://developer.apple.com/library/ios/documentation/uikit/reference/UIControl_Class/Reference/Reference.html#//apple_ref/occ/instm/UIControl/addTarget:action:forControlEvents:

Just include the events you want in the mask.