半钢琴应用程序与 UIButtons 的触摸问题

发布于 2024-12-18 05:02:57 字数 1211 浏览 2 评论 0原文

我正在开发一款半钢琴应用程序,其键盘布局与通常的键盘布局不同。

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶色山野 2024-12-25 05:02:57

我可能会完全放弃 UIButtons 并实现我自己的自定义触摸跟踪代码。请参阅处理多点触控事件文档。也就是说,您将实现以下方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

我可能只实现点击区域,您可以在 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:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

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.

半透明的墙 2024-12-25 05:02:57

恐怕,bensnider是对的。但我会通过 GestureRecognizer 实现它:

每个按键都有一个 TapRecognizer ,而父视图有一个 SwipeRecognizer 来检测从一个键到另一个键的滑动。

非常有用,Apple Developer Video Archive需要使用开发帐户登录

  • WWDC2010:会议 120 — 使用手势识别器简化触摸事件处理
  • WWDC2010:会议 121 — 高级手势识别

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:

  • WWDC2010: Session 120 — Simplifying Touch Event Handling with Gesture Recognizers
  • WWDC2010: Session 121 — Advanced Gesture Recognition
蓝眼睛不忧郁 2024-12-25 05:02:57

我会对所有按钮使用一个视图,并手动实现触摸跟踪,正如 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.

长亭外,古道边 2024-12-25 05:02:57

这是我制作的开源 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文