2009-10-27 10 views

risposta

0

Probabilmente si dovrebbe gestire l'UIControlTouchDown evento e seconda di ciò che si intende per "hold", sparare un NSTimer che conterà un intervallo da quando è stata avviata la touch e invalidare momento dello sparo o rilasciando il tocco (UIControlTouchUpInside e UIControlTouchUpOutside eventi). Quando il timer scatta, hai il "tap & premuto" rilevato.

+0

Sono lattina di non abbastanza esperto per venire da questa risposta al codice vero e proprio ... Ma io intendo per tenere la stesso comportamento in Mobile Safari quando si tocca e si tiene premuto un URL per visualizzare un foglio di azione per mostrare le opzioni relative a questo URL – JFMartin

6

Ecco il codice sollevato direttamente dalla mia app. È necessario aggiungere questi metodi (e un membro booleano _cancelTouches) a una classe derivata da UITableViewCell.

-(void) tapNHoldFired { 
    self->_cancelTouches = YES; 
    // DO WHATEVER YOU LIKE HERE!!! 
} 
-(void) cancelTapNHold { 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tapNHoldFired) object:nil]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    self->_cancelTouches = NO; 
    [super touchesBegan:touches withEvent:event]; 
    [self performSelector:@selector(tapNHoldFired) withObject:nil afterDelay:.7]; 
} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self cancelTapNHold]; 
    if (self->_cancelTouches) 
     return; 
    [super touchesEnded:touches withEvent:event]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self cancelTapNHold]; 
    [super touchesMoved:touches withEvent:event]; 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self cancelTapNHold]; 
    [super touchesCancelled:touches withEvent:event]; 
} 
+6

Non si dovrebbe mai usare un codice come questo auto -> _ cancelTouches = YES; Invece di usare solo self.cancelTouches = YES; e dichiarare proprietà private – Igor

+2

Qual è la sintassi "-> _"? mai visto prima :) –

6
//Add gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView): 

    // Add long tap for the main tiles 
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)]; 
    [tile addGestureRecognizer:longPressGesture]; 
    [longPressGesture release]; 

-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{ 
    NSLog(@"gestureRecognizer= %@",gestureRecognizer); 
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) { 
     NSLog(@"longTap began"); 

    } 

} 
Problemi correlati