【问题标题】:Continually sending messages from a UIButton不断从 UIButton 发送消息
【发布时间】:2013-03-08 00:02:39
【问题描述】:

我目前正在制作游戏,但无法创建方向键。在我点击一个按钮的那一刻,它只会运行一次,我不知道我可以调用哪个事件来让它不断运行代码,直到玩家停止触摸按钮。当我尝试使用 while 循环时,它只会使自身过载并阻止您触摸其他按钮。这是我通常用来移动播放器和实际加载按钮等的代码:

    - (void) playerMoveLeft
{    
    NSArray *images = [NSArray arrayWithObjects:img9,img10,img11,img12,img13,img14,img15,img16, nil];

    [imageView setAnimationImages:images];

        NSLog(@"Called left");
        CGRect myFrame = imageView.frame;
        myFrame.origin.x = imageView.frame.origin.x - 5;
        imageView.frame = myFrame;
        [imageView startAnimating];
    [NSTimer scheduledTimerWithTimeInterval:0.6
                                     target:self
                                   selector:@selector(nothingMovingLeft)
                                   userInfo:nil
                                    repeats:NO];
    [myTimer invalidate];
    myTimer = nil;
}


{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    img1 = [UIImage imageNamed:@"walking-e0000.png"];
    img2 = [UIImage imageNamed:@"walking-e0001.png"];
    img3 = [UIImage imageNamed:@"walking-e0002.png"];
    img4 = [UIImage imageNamed:@"walking-e0003.png"];
    img5 = [UIImage imageNamed:@"walking-e0004.png"];
    img6 = [UIImage imageNamed:@"walking-e0005.png"];
    img7 = [UIImage imageNamed:@"walking-e0006.png"];
    img8 = [UIImage imageNamed:@"walking-e0007.png"];
    img9 = [UIImage imageNamed:@"walking-w0000.png"];
    img10 = [UIImage imageNamed:@"walking-w0001.png"];
    img11 = [UIImage imageNamed:@"walking-w0002.png"];
    img12 = [UIImage imageNamed:@"walking-w0003.png"];
    img13 = [UIImage imageNamed:@"walking-w0004.png"];
    img14 = [UIImage imageNamed:@"walking-w0005.png"];
    img15 = [UIImage imageNamed:@"walking-w0006.png"];
    img16 = [UIImage imageNamed:@"walking-w0007.png"];

    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,180.0,80.0,80.0)];

    NSArray *images = [NSArray arrayWithObjects:img7, nil];

    [imageView setAnimationImages:images];

    [imageView setAnimationRepeatCount:100];

    [imageView setAnimationDuration:0.5];

    [imageView startAnimating];

    [myView addSubview:imageView];


    moveRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [moveRight addTarget:self action:@selector(playerMoveRight) forControlEvents:UIControlEventTouchUpInside];

    [moveRight setTitle:@"->" forState:UIControlStateNormal];

    moveRight.frame = CGRectMake(200, 280, 60, 60);

    [myView addSubview:moveRight];



    //longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(whichWayToMove:)];

    //longPress.minimumPressDuration = 0.0;

    //[myView addGestureRecognizer:longPress];

    moveLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [moveLeft addTarget:self action:@selector(playerMoveLeft) forControlEvents:UIControlEventTouchUpInside];

    [moveLeft setTitle:@"<-" forState:UIControlStateNormal];

    moveLeft.frame = CGRectMake(30, 280, 60, 60);

    [myView addSubview:moveLeft];

    NSLog(@"Finished setup");
}

【问题讨论】:

    标签: ios objective-c xcode cocoa-touch uibutton


    【解决方案1】:

    当您在playerMoveLeft 中使用计时器时,您应该在计时器中使用相同的方法重复自身,它只会根据您的代码运行一次。其次,您将重复设置为NO,以便重复使用YES

    [NSTimer scheduledTimerWithTimeInterval:0.6
                                         target:self
                                       selector:@selector(playerMoveLeft)
                                       userInfo:nil
                                        repeats:YES];
    

    【讨论】:

      【解决方案2】:

      使用NSTimer 多次调用特定的TIME INTERVAL

      [NSTimer scheduledTimerWithTimeInterval:2.0 // You can set TimeInterval as u need.
          target:self
          selector:@selector(targetMethod)
          userInfo:nil
          repeats:YES]; 
      

      2.0时间间隔调用targetMethod方法

      -(void)targetMethod
      {
         // write code for your method
      }
      

      您也可以使用invalidate的方法停止NSTimer

      [MyTimer invalidate];
      

      有关NSTimer的更多信息,请阅读How do I use NSTimer?

      【讨论】:

        【解决方案3】:

        阅读这篇文章,它既有计时器解决方案,也有带触摸解决方案的按钮

        Way to make a UIButton continuously fire during a press-and-hold situation?

        您可以根据自己的目的进行设置

        【讨论】:

          猜你喜欢
          • 2016-07-13
          • 1970-01-01
          • 2016-08-11
          • 1970-01-01
          • 2022-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-16
          相关资源
          最近更新 更多