半钢琴应用程序与 UIButtons 的触摸问题
我正在开发一款半钢琴应用程序,其键盘布局与通常的键盘布局不同。
我用 UIButtons 手动创建了视图, 我的问题是我不知道如何从一个 UIButton 滑动到另一个, 我通过 addTarget 和 withEvent 选项解决了这个问题,这让我能够访问触摸。
现在,在我添加这样的目标之后:(
[C addTarget:self action:@selector(outsideOfKey: forEvent:) forControlEvents:UIControlEventTouchDragOutside|UIControlEventTouchDragInside];
[C addTarget:self action:@selector(keyGetsLeft: forEvent:) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchUpInside];
也适用于所有其他键),
我设法使它们可滑动,
outsideOfKey:forEvent:
如下:(
-(void) outsideOfKey:(id)sender forEvent:(UIEvent *)event
{
for(UITouch *t in [event allTouches])
{
CGPoint touchPoint = [t locationInView:window];
if(CGRectContainsPoint(C.frame, touchPoint))
{
C.highlighted = YES;
}
else{
C.highlighted = NO;
}
对所有其他键完成的操作为出色地) 我可以从其他键滑入或滑入其他键,当我将它们留在 keyGetsLeft:forEvent
中时:我刚刚使用了相同的语法,而没有使用 else,并且突出显示的变成了 NO。
到这里就很简单了 但是,当我尝试进行多点触控时,我只能在周围滑动其中一个触摸,而其他触摸必须保持在同一位置。
更重要的是,如果我把一根手指拿开,所有手指都会变得不突出, 我知道这一切的原因,但我不知道如何解决它并使其发挥作用。
I am working on a semi-piano app with a diffrent keyboard-layout then a usual one.
I created the view manually with UIButtons,
My problem was I that I didn't know how to slide from a UIButton to another,
I figured that out with addTarget with the option of withEvent, which gave me the access to the touches.
Now, after I added the target like this:
[C addTarget:self action:@selector(outsideOfKey: forEvent:) forControlEvents:UIControlEventTouchDragOutside|UIControlEventTouchDragInside];
[C addTarget:self action:@selector(keyGetsLeft: forEvent:) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchUpInside];
(also for all of the other keys),
I maneged to make them slideable,
outsideOfKey:forEvent:
is as follows:
-(void) outsideOfKey:(id)sender forEvent:(UIEvent *)event
{
for(UITouch *t in [event allTouches])
{
CGPoint touchPoint = [t locationInView:window];
if(CGRectContainsPoint(C.frame, touchPoint))
{
C.highlighted = YES;
}
else{
C.highlighted = NO;
}
(Done for all the other keys as well)
I can slide from and into other keys, and when I leave them in keyGetsLeft:forEvent
: I have just used the same syntx without the else, and the highlighted became NO.
Up to here it's easy,
But then when I try to do multi-touch, I can slide only one of the touches all around and the others must stay in the same position.
And even more, If I take one of the fingers away all of them are becoming non-highlighted,
I know the reasons to all of that, but I don't know how to fix it and make it to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可能会完全放弃 UIButtons 并实现我自己的自定义触摸跟踪代码。请参阅处理多点触控事件文档。也就是说,您将实现以下方法:
我可能只实现点击区域,您可以在 IB 中设置它,也许使用自定义 UIView 触摸覆盖(只是带有自定义子类的 UIView)。这将允许您在 IB 中设置带有图像、标题等的视图,但在自定义子类中执行所有触摸跟踪。
I would probably move away from UIButtons altogether and implement my own custom touch tracking code. See Handling Multitouch Events in the docs. Namely, you will be implementing the following methods:
I would probably just implement hit areas, which you could setup in IB, with perhaps a custom UIView touch overlay (just a UIView with a custom subclass). This would let you setup the view in IB with images, titles, etc., but do all of your touch tracking in your custom subclass.
恐怕,bensnider是对的。但我会通过 GestureRecognizer 实现它:
每个按键都有一个 TapRecognizer ,而父视图有一个 SwipeRecognizer 来检测从一个键到另一个键的滑动。
非常有用,Apple Developer Video Archive需要使用开发帐户登录:
I am afraid, bensnider is right. But I'd implement it via GestureRecognizer:
Each key has one TapRecognizer, while a parent view has a SwipeRecognizer to detect slides form one key to another.
Very useful, on Apple Developer Video Archivelogin with development account required:
我会对所有按钮使用一个视图,并手动实现触摸跟踪,正如 bensnider 所说。手动绘制背景/标题也不是那么困难。
I'd use one view for all buttons and manually implement touch tracking as bensnider said. Manually drawing backgrounds/titles also is not so difficult.
这是我制作的开源 iOS 钢琴应用程序的链接 https://github.com/meech-ward/iOSPiano
我使用 CALayers 作为钢琴键,并且使用
<代码>
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
检测用户当前是否正在触摸钢琴键。
即使多次触摸,它也能很好地工作。
Heres a link to an open source iOS piano app I made https://github.com/meech-ward/iOSPiano
I used CALayers for the piano keys and I used
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
to detect if the user is currently touching a piano key.
It works really well even with multiple touches.