1

I am trying to code a controller for an image view to move using buttons.

inside up button code :

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

code is working properly but I have to tap it repeatedly to keep moving up, I wanna know which method to call, to move it, while holding the up button.

edit :

-(void)touchDownRepeat{

        mainChac.center = CGPointMake(mainChac.center.x, mainChac.center.y - 5);

}

-(IBAction)upButton:(id)sender{

    [upButton addTarget:self action:@selector(touchDownRepeat) forControlEvents:UIControlEventTouchDownRepeat];

}

edit 2: solution

-(void)moveUp{
    mainChac.center = CGPointMake(mainChac.center.x, mainChac.center.y - 5);

}

- (void)holdDown
{
    NSLog(@"hold Down");

    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveUp) userInfo:nil repeats:YES];

}

- (void)holdRelease
{
    NSLog(@"hold release");
    [timer invalidate];

}

-(IBAction)upButton:(id)sender{

    [upButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
    [upButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];

}
shaideru
  • 45
  • 1
  • 6

1 Answers1

1

EDIT:

What you need to to have 2 events, one for while the button is pressed down and another for when the button is released like so:

[aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
[aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];


 - (void)holdDown
  {
     NSLog(@"hold Down");
  }

  - (void)holdRelease
  {
      NSLog(@"hold release");

  }

Have your hold down function start a loop, and your holdRelease stop the loop.

EDIT-2:

An easy way (but perhaps not the best way) to achieve this loop would be to use NSTImer scheduledTimerWithTimeInterval inside the hold down and invalidate it in the release method.

all of this stuff has been done before, please try googling this stuff. Here is a stackoverlfow question with an example: ios scheduledTimerWithTimeInterval for amount of time

Community
  • 1
  • 1
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
  • I already saw that link, I can't figure out on how to move an image while holding upButton. – shaideru Feb 05 '14 at 14:01
  • @frustratediOSdeveloper `touchDownRepeat` is called multiple times, set it to that, and as you hold down your finger it will keep calling and run the code – Simon McLoughlin Feb 05 '14 at 14:29
  • @frustratediOSdeveloper please edit your question and add the code there, thats very difficult to read – Simon McLoughlin Feb 05 '14 at 14:33
  • I tried it, still doesn't work. what's happening is that I have to tap the button multiple times to move the image. I edited the code :) – shaideru Feb 05 '14 at 14:35
  • @frustratediOSdeveloper `touchDownRepeat` is not a delegate! when you created `upButton` inside InterfaceBuilder you assigned it to the **event** `touchUpInside`. Delete the connection, create a new connection and when the popup shows, don't select `touchUpInside` select `touchDownRepeat ` – Simon McLoughlin Feb 05 '14 at 14:39
  • touch down repeat works if I tap on the button twice quickly then the code inside the scope will work, but if I just tap the button and hold it, nothing will happen. – shaideru Feb 05 '14 at 14:46
  • I am really sorry but I can't figure the loop out. can you provide an example. – shaideru Feb 05 '14 at 15:17
  • thanks! NSTimer did it, I suppose it's not the best way, but it works now! thanks again! – shaideru Feb 05 '14 at 15:37
  • @frustratediOSdeveloper no prob, happy coding – Simon McLoughlin Feb 05 '14 at 15:39