2013-01-18 54 views

回答

1

我只是谷歌它,我得到了最好的答案從堆棧溢出This

- (void)viewDidLoad 
{ 
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 
    [self.button addGestureRecognizer:longPress]; 
    [longPress release]; 

[super viewDidLoad]; 


} 

和事件: -

- (void)longPress:(UILongPressGestureRecognizer*)gesture { 
    if (gesture.state == UIGestureRecognizerStateEnded) { 
     NSLog(@"Long Press"); 
    } 
} 
+0

我的問題是存在自定義單元格 – user1987342

+0

沒有viewDidLoad中它不是nesecory在只有viewDidLoad中添加此方法。在你的自定義單元格中,只需要在cellForRowAtindex等中添加按鈕時,在你的costome單元格代碼中添加上面的代碼 –

+0

分享你的代碼我將編輯它 –

0

您可以通過創建和連接的UILongPressGestureRecognizer實例的按鈕開始。

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 
[self.button addGestureRecognizer:longPress]; 
[longPress release]; 

,然後實現它處理手勢

- (void)longPress:(UILongPressGestureRecognizer*)gesture { 
    if (gesture.state == UIGestureRecognizerStateEnded) { 
     NSLog(@"Long Press"); 
    } 
} 

現在,這將是基本方法的方法。您還可以設置印刷機的最短持續時間和可承受的誤差。另外請注意,如果您在識別該手勢後幾次調用該方法,那麼如果您想在其末尾執行某些操作,則必須檢查其狀態並處理它。

Reference

0
- (void)setLongTouchAction:(SEL)newValue 
{ 
    if (newValue == NULL) 
    { 
     [self removeGestureRecognizer:longPressGestureRecognizer]; 
     [longPressGestureRecognizer release]; 
     longPressGestureRecognizer = nil; 
    } 
    else 
    { 
     [longPressGestureRecognizer release]; 
     longPressGestureRecognizer = nil; 

     longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:[[self allTargets] anyObject] action:newValue]; 
     [self addGestureRecognizer:longPressGestureRecognizer]; 
    } 
} 


[undoButton addTarget:self action:@selector(performUndo:) forControlEvents:UIControlEventTouchUpInside]; 
[undoButton setLongTouchAction:@selector(showUndoOptions:)];