2016-05-19 7 views
0

私はSWTableViewCellを使用していると私は、ユーザーのスワイプを左または右ときにトリガーされるアクションを知りたいです。あなたはあなたの中にジェスチャー認識機能を追加する必要がのUITableViewCellは、どのように私は、ユーザーが左または右スワイプしたことを検出することができますか?

+0

がこれをチェックします。http ://stackoverflow.com/questions/6167756/how-to-detect-a-swipe-to-delete-gesture-in-a-customized-uitableviewcell – kb920

+0

このアプローチは 'UIGestureRecognizerStateEnded'州だけを認識します –

+0

@ahmedlabib' UIGestureRecognizerState'には、開始、キャンセル、変更、終了などのように複数のオプションがあります –

答えて

4

UISwipeGestureRecognizer* swRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)]; 
[swRight setDirection:UISwipeGestureRecognizerDirectionRight]; 
[cell addGestureRecognizer:swRight]; 

UISwipeGestureRecognizer* swLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedLeft:)]; 
[swLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
[cell addGestureRecognizer:swLeft]; 

をcellForRowAtIndexPathし、その選択方法

-(void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer 
{ 
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     // your code 
    } 
} 

-(void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer 
{ 
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     // your code 
    } 
} 
+0

働い@wolverineありがとうございました。 –

+0

いいえ...「UIGestureRecognizerStateBegan」、「UIGestureRecognizerStateCancelled」、「UIGestureRecognizerStateChanged」、「UIGestureRecognizerStateEnded」など –

+0

あなたに感謝し、この[SWTableViewCell](https://(SWTableViewCell *)セルスクロールToState:(SWCellState)状態; ' –

1

それあなたのために働くだろう、このコードを試してみてください。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)]; 
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight]; 
    [cell addGestureRecognizer:swipe]; 

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row]; 


return cell; 
    } 


    - (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer 
    { 
     if (gestureRecognizer.state == UIGestureRecognizerStateEnded) 
     { 
      UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view; 
      NSIndexPath* indexPath = [self.tableView indexPathForCell:cell]; 
      //.. 
     } 
    } 
関連する問題