Search code examples
iphonetouchesbegantouchesmoved

Touchesbegan always fire when using touchesmoved?


I'm new to iPhone programing. I'm trying to do a simple window with a cat doing two sounds. When you click at the cat icon it should do "miaau", and when wou drag over window (stroke) it should make "mrrrr". It works, but always when I try to make cat mrrrrr function TouchesBegan fires and cat also do "miaaau".

What to do to make the interface recognize I want only stroke cat, not to touch it for doing the first option, "miaau"?


Solution

  • I suggest to add a NSTimer to touchesBegan method with small time interval (say 0.1 sec):

    BOOL tap_event = NO; //ivar declared in header
    
    -(void) touchesBegan:... {
        tap_event = YES;
        [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(checkTap:) userInfo: nil repeats: NO];
    } 
    
    -(void) checkTap:(NSTimer*) t {
         if( tap_event ) //miauu here
         tap_event = NO;
    }
    
    -(void) touchesMoved:... {
        tap_event = NO;
         //mrrrr here
    }
    

    Or as an option check docs for UIGestureRecognizers