用按钮触摸移动?

发布于 2024-08-21 09:24:04 字数 77 浏览 7 评论 0原文

是否可以使用带有按钮的 touchesMoved 函数而不是 UIImageView

Is it possible to use the touchesMoved function with buttons instead of UIImageViews?

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

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

发布评论

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

评论(2

梨涡 2024-08-28 09:24:04

是的。

在 .h 文件

IBOutlet UIButton *aButton;

中 在 .m 文件中

    -(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];
    }
}

最后,在 Interface Builder 中,将按钮连接到“aButton”并关闭按钮的“用户交互启用”。
这很重要,因为它允许 TouchMoved 来处理它。

我已经调整了上面的代码来检查按钮突出显示的状态。这是为了防止当您将手指拖动到该区域时多次触发按钮。

要让“钢琴键”在点击时起作用,请使用 -(void)touchesBegan

要将按钮突出显示状态设置回 = NO;,请使用 -(void)touchesEnded

我必须找出你所追求的完全相同的东西。我无法弄清楚 Touch Drag Enter

因此,为了避免就该主题发表多个帖子,请查看我的问题和答案。

问题 1

问题 2

Yes.

In your .h file

IBOutlet UIButton *aButton;

In your .m file

    -(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];
    }
}

And finally, in Interface Builder, connect your button to 'aButton' and turn off "User Interaction Enabled" for your button.
This is important because it allow touchesMoved to handle it.

I have adjusted the code above to check the buttons highlighted state. This is to stop it from firing the button multiple times when you drag your finger inside the area.

To get your "piano keys" to work when you tap them, use -(void)touchesBegan

To set your button highlight states back to = NO;, use -(void)touchesEnded

I have had to find out the exact same thing you are after. I couldn't figure out Touch Drag Enter

So to avoid multiple posts on the subject, please check out my question and answers.

question 1

question 2

帅气称霸 2024-08-28 09:24:04

您肯定会发现当前正在触摸事件的“视图”正在被拖动,即使它是一个按钮。您还可以在 Interface Builder 中使用按钮的“触摸拖动输入”连接。选择按钮后按 cmd+2 查看连接选项卡。

You could certainly find the 'view' that is currently being touch event is dragging over even if it is a button. You could also use the 'touch drag enter' connection for buttons in the Interface Builder. Look at the connections tab by pressing cmd+2 when having selected a button.

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