views:

284

answers:

3

Hi,

I am using - (void) touchesMoved to do stuff when ever I enter a specific frame, in this case the area of a button.

My problem is, I only want it to do stuff when I enter the frame - not when I am moving my finger inside the frame.

Does anyone know how I can call my methods only once while I am inside the frame, and still allow me to call it once again if I re-enter it in the same touchMove.

Thank you.

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self.view] anyObject];

    CGPoint location = [touch locationInView:touch.view];

    if(CGRectContainsPoint(p1.frame, location))
    {
            //I only want the below to me called 
            // once while I am inside this frame
        [self pP01];
        [p1 setHighlighted:YES];
    }else {
        [p1 setHighlighted:NO];
    }
}
+1  A: 

You can use some attribute to check if the code was already called when you were entering specific area. It looks like highlighted state of p1 object (not sure what it is) may be appropriate for that:

if(CGRectContainsPoint(p1.frame, location))
{
   if (!p1.isHighlighted){ // We entered the area but have not run highlighting code yet
        //I only want the below to me called 
        // once while I am inside this frame
      [self pP01];
      [p1 setHighlighted:YES];
   }
}else { // We left the area - so we'll call highlighting code when we enter next time
    [p1 setHighlighted:NO];
}
Vladimir
+1  A: 

Simply add a BOOL that you check in touchesMoved and reset in touchesEnded

willcodejavaforfood
you need to reset this bool flag also when you exit the required area: see "and still allow me to call it once again if I re-enter it in the same touchMove." requirement in the question
Vladimir
@Vladimir - Thanks, sloppy reading by me there
willcodejavaforfood
A: 

if( CGRectContainsPoint([p1 frame],[touch locationInView:self.view])) { NSLog (@"Touch Moved over p1"); if (!p14.isHighlighted) { [self action: p1]; p1.highlighted = YES; } }else { p1.highlighted = NO; }

Morbo