可能重複:通過表自定義cell.How
How to detect tap on Uitableview cell with uiview and uibutton?
UIButton Long Press Event長按事件
我加載按鈕後才能確定我是否用戶的單次點擊或長按事件按鈕?
可能重複:通過表自定義cell.How
How to detect tap on Uitableview cell with uiview and uibutton?
UIButton Long Press Event長按事件
我加載按鈕後才能確定我是否用戶的單次點擊或長按事件按鈕?
我只是谷歌它,我得到了最好的答案從堆棧溢出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");
}
}
我的問題是存在自定義單元格 – user1987342
沒有viewDidLoad中它不是nesecory在只有viewDidLoad中添加此方法。在你的自定義單元格中,只需要在cellForRowAtindex等中添加按鈕時,在你的costome單元格代碼中添加上面的代碼 –
分享你的代碼我將編輯它 –
您可以通過創建和連接的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");
}
}
現在,這將是基本方法的方法。您還可以設置印刷機的最短持續時間和可承受的誤差。另外請注意,如果您在識別該手勢後幾次調用該方法,那麼如果您想在其末尾執行某些操作,則必須檢查其狀態並處理它。
- (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:)];
你可以用'LongpressGestureRecognizer' ... – Venkat