Friday, April 24, 2015

objective-c gesture recognizer calling multiple time till state end

In viewdidLoad

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPress.minimumPressDuration=1.0f;

    [self.deleteDigitButton addGestureRecognizer:longPress];


Long press button
timer is being used to call itself through longPress timer function

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    
    if( gesture.state == UIGestureRecognizerStateBegan ) {
        NSLog(@"DialerViewController.Long Press");
        
        NSString *digits = [PhoneNumberFormatter unformat: self.TextField.text];
        if(digits.length > 0 && (gesture.state != UIGestureRecognizerStateEnded || gesture.state != UIGestureRecognizerStateCancelled || gesture.state != UIGestureRecognizerStateFailed)) {
            digits = [digits substringToIndex:digits.length - 1];
            self.TextField.text = [PhoneNumberFormatter format:digits];
            [self.TextField setNeedsDisplay];
            
            NSTimeInterval timeInterval = 0.1; //seconds
            [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                             target:self
                                           selector:@selector(longPressTimer:)
                                           userInfo:gesture
                                           repeats:NO];
            
        }
    }
}

longPressTimer function
-(void)longPressTimer:(NSTimer *)timer{
    UILongPressGestureRecognizer *gesture = [timer userInfo];
    [self longPress:gesture];
}

No comments:

Post a Comment