tags:

views:

198

answers:

3

I want to be able to repeat the code inside my touchBegan code that activates when you click the UIImageView but will not repeat itself while your finger is down... How do I make it so while my finger is pressing the UIImageView it repeats itself.

A: 

Oh I must of worded it wiered... I "DO" want the code to repeat while the button is being pressed "Down"and when the user takes his finger off the button the code stops running.

In the future, please try to reply to answers using comments, rather than with another answer.
Tim
A: 

You could track whether or not your view has received a touchBegan event without a corresponding touchEnded. If this is the case, you can infer that a touch is going on right now, and continue running the code. For example:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.touchInProgress = YES; // Assuming you have some property touchInProgress
    [self runSomeCode];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    self.touchInProgress = NO;
}

- (void)runSomeCode {
    // Do your code here

    if(self.touchInProgress) {
        [self runSomeCode]
    }
}

This example doesn't take into account a number of things, including the touchesCancelled method, and the relevant threading (if you're always running "some code", I'm not sure, but you may never get the touchesEnded method to run - play with performSelector:afterDelay: or performSelectorInBackground:).

Tim
ya tried all that code and didn't get any warnings or errors but still wasn't repeating the code I wanted as it was being held down :/ guess it's back to the drawing board.
+1  A: 

Seeing your clarification, I think you want something like this. This will call the timerFired: method every PERIOD seconds while the finger is down.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // timer is an ivar...
    timer = [[NSTimer scheduledTimerWithTimeInterval:PERIOD
                                              target:self
                                            selector:@selector(timerFired:)
                                            userInfo:nil
                                             repeats:YES]
             retain];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [timer invalidate];
    [timer release];
    timer = nil;
}

- (void)timerFired:(NSTimer *)theTimer
{
    // do stuff
}
Daniel Dickison