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