TouchesMoved 与 Button?

TouchesMoved with Button?(TouchesMoved 与 Button?)
本文介绍了TouchesMoved 与 Button?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 touchesMoved 函数与按钮一起使用而不是 UIImageViews?

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

推荐答案

是的.

在您的 .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"并为您的按钮关闭启用用户交互".这很重要,因为它允许 touchesMoved 处理它.

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.

要让您的钢琴键"在您敲击时发挥作用,请使用 -(void)touchesBegan

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

要将按钮高亮状态设置回 = NO;,请使用 -(void)touchesEnded

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

我不得不找出你所追求的完全相同的东西.我想不通 Touch Drag Enter

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.

问题1

问题2

这篇关于TouchesMoved 与 Button?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)
iOS: Send multiple actions to a bar button(IOS:向一个栏按钮发送多个动作)
UIBarButton won#39;t show in my UINavigationController(UIBarButton不会在我的UINavigationController中显示)
appearance().setBackgroundImage Not Working On Custom Class(外观().setBackoundImage在自定义类上不起作用)
How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)