在obj-c中连续按下按钮时如何重复执行动作 [英] How to repeatedly perform an action when a button is continuously pressed in obj-c

查看:70
本文介绍了在obj-c中连续按下按钮时如何重复执行动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个简单的方法,四个按钮和一个对象。

I've got four simple methods, four buttons and one object.

- (IBAction)left:(id)sender{
    object.center = CGPointMake(object.center.x - 5, object.center.y);
}

- (IBAction)right:(id)sender{
    object.center = CGPointMake(object.center.x + 5, object.center.y);
}
- (IBAction)down:(id)sender{
    object.center = CGPointMake(object.center.x, object.center.y + 5);
}
- (IBAction)up:(id)sender{
    object.center = CGPointMake(object.center.x, object.center.y - 5);
}

当我按下一个按钮时,该方法执行一次。连续按下该按钮时,它是相同的。
该怎么办,以便当我连续按下按钮时,我的对象不断向左移动?

When I press a button, the method is executed once. When the button is pressed continuously it's the same. What do I have to do so that when I press the button continuously my object keep moving to the left?

推荐答案

正如@Maudicus所说,您可能需要使用 NSTimer s进行某些操作,以获取连续的按钮按下触发。我要使用的示例是在屏幕上移动对象(因为无论如何都想这样做)。我使用了分级移动,因为我不知道您是否正在编写基于网格的游戏,因此需要精确的5像素移动。只需切出所有stepSize代码,然后将其设置为5即可。

As @Maudicus said, you probably need to do something with NSTimers to get a continuous button press triggering. The example I'm going to use is moving an object on screen (since you want to do that anyway). I've used graded moving because I have no idea whether you are writing a grid-based game and so need exactly 5 pixels of movement. Just cut out all of the stepSize code and set it to 5 if that's what you do.


  1. 编写一个计时器回调函数检查是否设置了 BOOL ,如果设置了则继续触发自身:

  1. Write a timer callback function which checks whether a BOOL is set and if so keeps triggering itself:

- (void)moveObjectLeft:(NSTimer *)timer
{
    // check the total move offset and/or the X location of the object here
    // if the object can't be moved further left then invalidate the timer
    // you don't need to check whether the button is still being pressed
    //[timer invalidate];
    //return;

    // the object moves gradually faster as you hold the button down for longer
    NSNumber *moveOffset = (NSNumber *)[timer userInfo];
    NSUInteger stepSize = 1;
    if(moveOffset >= 40)
        stepSize = 10;
    else if(moveOffset >= 15)
        stepSize = 5;
    else if(moveOffset >= 5)
        stepSize = 2;

    // move the object
    object.center = CGPointMake(object.center.x - stepSize, object.center.y);

    // store the new total move offset for this press
    moveOffset += stepSize;
    [timer setUserInfo:moveOffset];
}


  • 在当前类中创建一个计时器属性 .h

    @property (nonatomic, retain) NSTimer *moveTimer;
    


  • 在您的 .m :

    @synthesize moveTimer;
    


  • 在按下按钮时创建计时器对象。在 touchesBegan:withEvent:中执行此操作并检查它是否为Touch Down事件,或者将Interface Builder中的Touch Down事件连接到 IBAction 方法。

  • Create your timer object when the button is pressed. Either do this in touchesBegan:withEvent: and check it's a Touch Down event, or connect the Touch Down event in Interface Builder to an IBAction method.

    NSNumber *moveOffset = [NSNumber numberWithUnsignedInt:0];
    self.moveTimer =
        [NSTimer
         scheduledTimerWithTimeInterval:0.2
         target:self
         selector:@selector(moveObject:)
         userInfo:moveOffset
         repeats:YES];
    


  • 释放按钮时(再次使用上述方法之一, touchesEnded:withEvent:进行内部触摸甚至可能进行外部触摸,或者在这两个事件上使用另一个 IBAction ),均无效外部计时器:

  • When the button is released (using one of the above methods again, touchesEnded:withEvent: for a Touch Up Inside or probably even a Touch Up Outside, or another IBAction on both of those events), invalidate the timer externally:

    [self.moveTimer invalidate];
    self.moveTimer = nil;
    


  • 这篇关于在obj-c中连续按下按钮时如何重复执行动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆