2009-10-27 12 views

Odpowiedz

0

Należy prawdopodobnie obsługiwać UIControlTouchDown zdarzenie iw zależności od tego, co masz na myśli przez „trzymaj”, ogień NSTimer która będzie liczyć odstęp od zainicjowania w dotyku i unieważnić po wypaleniu lub zwalniając dotyk (UIControlTouchUpInside i UIControlTouchUpOutside zdarzeń). Po uruchomieniu timera zostanie wykryte zatrzymanie "dotknij &".

+0

jestem puszka nie wystarczająco eksperta pochodzić z tej odpowiedzi do rzeczywistego kodu ... ale chodzi mi o samo zachowanie trzymać w Mobilny Safari po dotknięciu i przytrzymaniu adresu URL, aby wyświetlić arkusz akcji, pokazujący opcje dotyczące tego adresu URL – JFMartin

6

Oto kod przeniesiony prosto z mojej aplikacji. Powinieneś dodać te metody (i boolean _cancelTouches member) do klasy, którą wyprowadzasz z 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

Nigdy nie należy używać takiego kodu: self -> _ cancelTouches = YES; Zamiast po prostu użyć self.cancelTouches = YES; i zadeklaruj własność prywatną – Igor

+2

Jaka jest ta składnia "-> _"? nigdy tego nie widziałem :) –

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"); 

    } 

} 
Powiązane problemy