简体   繁体   English

如何检测进入UIButton框架的触摸?

[英]How can I detect a touch entering the frame of a UIButton?

I'm writing a class that implements a piano keyboard. 我正在写一个实现钢琴键盘的课程。 Right now it's just a UIView and each key is a UIButton. 现在它只是一个UIView,每个键都是UIButton。 I'd like the user to be able to drag their finger and hit several keys, but right now it only registers touch down. 我希望用户能够拖动他们的手指并点击几个键,但是现在它只能记录触摸。 I've added UIControlEventTouchDragEnter as a way to trigger the key event but it only works if you hit a key, drag your finger elsewhere, and drag it back to the same key. 我添加了UIControlEventTouchDragEnter作为触发键事件的方法,但它只有在你按一个键,将手指拖到其他地方并将其拖回到同一个键时才有效。

Is there a way to detect drag events with UIButtons? 有没有办法用UIButtons检测拖动事件? Will I have to start over and use something else for each key? 我是否必须重新开始并为每个键使用其他东西? I don't want to calculate which key the touch is on based on its coordinates because that seems like it should be unnecessary. 我不想根据其坐标来计算触摸所在的键,因为这看起来应该是不必要的。 What do you think is the simplest way to do this? 您认为最简单的方法是什么?

Thanks, 谢谢,

Luke 卢克

您可以跟踪最后按下按钮的UIControlEventTouchUpOutside,以及它的处理程序检查,用户移动他/她的手指。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM