2009-10-27 85 views

回答

0

你或許應該處理UIControlTouchDown事件,這取決於你的意思是「持有」的東西,火的NSTimer,因爲你發起的觸摸,將計算的時間間隔,並在燒製時失效或釋放觸摸(UIControlTouchUpInsideUIControlTouchUpOutside events)。當定時器啓動時,您會檢測到「輕按&」。

+0

我沒有足夠的專家來自這個答案實際代碼都可以......但我的意思是在保持相同的行爲移動Safari瀏覽器時,點擊並持有URL以彈出操作表,以顯示有關此URL的選項 – JFMartin 2009-10-29 00:40:18

6

下面是直接從我的應用程序解除的代碼。您應該將這些方法(和一個布爾_cancelTouches成員)添加到您從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

您不應該使用類似此類的代碼 self - > _ cancelTouches = YES; 而不是僅僅使用 self.cancelTouches = YES; 並聲明屬性私人 – Igor 2010-04-12 11:58:23

+2

這是什麼語法「 - > _」?從未見過它:) – 2010-09-29 03:03:11

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

    } 

} 
相關問題