您提供的链接中接受的答案适用于两个滑动方向。
请注意,gestureRecognizer.direction 为 UISwipeGestureRecognizerDirectionLeft 和 UISwipeGestureRecognizerDirectionRight 返回 YES。
您只需要修改几处:
更改在滑动时调用的选择器,因此它将调用您的方法,而不是帖子示例中的方法,
并将滑动方向更改为仅从左到右,而不是当前的两个方向,因为据我了解,您正在尝试设置单向滑动。
所以你的代码应该是这样的:
// In cellForRowAtIndexPath:, where you create your custom cell
cell.tableView=tableView;
cell.indexPath=indexPath;
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(YOUR_METHOD_GOES_HERE)];
[cell addGestureRecognizer:swipeGestureRecognizer];
。
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]] && ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)
return YES;
}
请注意,您也可以使用已接受答案下方的答案,只需将手势识别器direction 属性修改为UISwipeGestureRecognizerDirectionRight,而不是示例中的当前方向,即UISwipeGestureRecognizerDirectionLeft。
如果您选择实现这一点,您的 viewController 必须实现手势识别器委托,您的代码应如下所示:
// Call this method in viewDidLoad
- (void)setUpLeftSwipe {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeRightt:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[self.tableView addGestureRecognizer:recognizer];
recognizer.delegate = self;
}
- (void)swipeRight:(UISwipeGestureRecognizer *)gestureRecognizer {
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
... do something with cell now that i have the indexpath, maybe save the world? ...
}
注意——如果我没记错的话,你需要自己创建单元格滑动动画,因为我相信 Xcode 的默认单元格动画只有在向左滑动时才会出现。
信用来自您提供的链接的MadhavanRP 和Julian。我刚刚修改了他们的答案以更好地满足您的需求。
不过,我自己还没有尝试并实现过。