如何让多个触摸按钮与 TouchBegan/Moved 一起使用?

发布于 2024-08-22 04:09:10 字数 2067 浏览 6 评论 0原文

我有一堆按钮,我想以三种不同的方式激活它们。

  1. Touch Down
  2. Touch Down - 多次触摸(同时)
  3. Touch Drag Inside(与在钢琴上拖动手指相同)

前两个在 IB 中显然很容易。然而,很多人,包括我自己,都在使用 Touch Drag 时遇到了麻烦。所以我最终使用 - (void) TouchMoved [参见代码] 。这对于拖动来说非常有用...但是为了让它工作,我必须禁用 IB 中的“用户交互”按钮。这意味着我失去了“Touch Down”和多点触控功能。

因此,为了让“Touch Down”发挥作用,我使用了 -(void) TouchsBegan [参见代码]。这工作正常,但我无法让多点触控工作。

有谁知道如何让我的按钮在多点触控期间同时触发?
或者...有没有办法让IB中的触摸移动和按钮功能一起工作?

我已经尝试过 touch.view.multiTouchEnabled = Yes; 并确保我的按钮在 IB 中可以多次触摸...但什么也没有。

下面是我的代码。 非常感谢您的帮助。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self.view] anyObject];

    CGPoint location = [touch locationInView:touch.view];

    if(CGRectContainsPoint(p1.frame, location)) 
    {
        if (!p1.isHighlighted){
            [self pP01];
            [p1 setHighlighted:YES];
    }
}else {
        [p1 setHighlighted:NO];
    }
    //
    if(CGRectContainsPoint(p2.frame, location)) 
    {
        if (!p2.isHighlighted){
            [self pP02];
            [p2 setHighlighted:YES];
        }
    }else {
        [p2 setHighlighted:NO];
    }
    if(CGRectContainsPoint(p3.frame, location))
    {
        if (!p3.isHighlighted){
            [self pP03];
            [p3 setHighlighted:YES];
        }
    }else {
        [p3 setHighlighted:NO];
    }
}

///

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [[event touchesForView:self.view] anyObject];
        CGPoint location = [touch locationInView:touch.view];

        if(CGRectContainsPoint(p1.frame, location))
        {
            [self pP01];
            [p1 setHighlighted:YES];
        }
        if(CGRectContainsPoint(p2.frame, location))
        {
            [self pP02];
            [p2 setHighlighted:YES];
        }
        if(CGRectContainsPoint(p3.frame, location))
        {
            [self pP03];
            [p3 setHighlighted:YES];
        }
}

I have a bunch of buttons that I want to activate in three different ways.

  1. Touch Down
  2. Touch Down - multiple touch (at the same time)
  3. Touch Drag Inside (The same as dragging your finger over a piano)

The first two is obviously easy in IB. However many people, including myself, have had trouble with Touch Drag inside. So I ended up using - (void) touchesMoved [see code] . This works great for the drag... but to get it to work I had to disable the buttons "user Interaction" in IB. Which means I lost the "Touch Down" and multi-touch capabilities.

So, in order to get the "Touch Down" to work, I used -(void) touchesBegan [see code]. This works fine, but I can't get multi-touch to work.

Does anyone know how I can get my buttons to fire simultaneously during multi-touch?
Or ... Is there a way to get touches moved and button functions in IB to work together?

I have tried touch.view.multiTouchEnabled = Yes; and I have ensured that my buttons are multiple touch ok in IB... But nothing.

Below is my code.
Thank you very much for your help.

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self.view] anyObject];

    CGPoint location = [touch locationInView:touch.view];

    if(CGRectContainsPoint(p1.frame, location)) 
    {
        if (!p1.isHighlighted){
            [self pP01];
            [p1 setHighlighted:YES];
    }
}else {
        [p1 setHighlighted:NO];
    }
    //
    if(CGRectContainsPoint(p2.frame, location)) 
    {
        if (!p2.isHighlighted){
            [self pP02];
            [p2 setHighlighted:YES];
        }
    }else {
        [p2 setHighlighted:NO];
    }
    if(CGRectContainsPoint(p3.frame, location))
    {
        if (!p3.isHighlighted){
            [self pP03];
            [p3 setHighlighted:YES];
        }
    }else {
        [p3 setHighlighted:NO];
    }
}

///

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [[event touchesForView:self.view] anyObject];
        CGPoint location = [touch locationInView:touch.view];

        if(CGRectContainsPoint(p1.frame, location))
        {
            [self pP01];
            [p1 setHighlighted:YES];
        }
        if(CGRectContainsPoint(p2.frame, location))
        {
            [self pP02];
            [p2 setHighlighted:YES];
        }
        if(CGRectContainsPoint(p3.frame, location))
        {
            [self pP03];
            [p3 setHighlighted:YES];
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

最舍不得你 2024-08-29 04:09:10

您需要检查每次触摸,而不是随机触摸。因此,for(UITouch *t in Touchs) 而不是 UITouch *touch = [touches anyObject]

You need to check every touch instead of one random touch. So, for(UITouch *t in touches) instead of UITouch *touch = [touches anyObject]

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