我有一个UITableViewCell类,我想检测滑动事件(删除),以隐藏drawRect
中绘制的一些图形
首先我在单元格中添加了UISwipeGestureRecognice
:
// Init swipe gesture recognizer
self.swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeCell:)];
self.swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
self.swipeRecognizer.delegate = self;
[self.contentView addGestureRecognizer:self.swipeRecognizer];
我实现了一个方法来对滑动事件作出反应:
- (void)swipeCell:(UISwipeGestureRecognizer *)recognizer
{
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
self.swipeStartPoint = [recognizer locationInView:self.backgroundView];
BaseLogDebug(INFO, @"Swipe Began at %@", NSStringFromCGPoint(self.swipeStartPoint));
break;
case UIGestureRecognizerStateChanged: {
CGPoint currentPoint = [recognizer locationInView:self.backgroundView];
CGFloat deltaX = currentPoint.x - self.swipeStartPoint.x;
BaseLogDebug(INFO, @"Swipe Moved %f", deltaX);
}
break;
case UIGestureRecognizerStateEnded:
BaseLogDebug(INFO, @"Swipe Ended");
break;
case UIGestureRecognizerStateCancelled:
BaseLogDebug(INFO, @"Swipe Cancelled");
break;
default:
break;
}
}
为了允许同时手势识别器,我实现了以下方法:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
手势识别识别的唯一状态是状态UIGestureRecognizerStateEnded
。我的代码出了什么问题?
答案 0 :(得分:1)
来自UIGestureRecognizer Class Reference docs:
离散手势的识别器从UIGestureRecognizerStatePossible转换为UIGestureRecognizerStateFailed或UIGestureRecognizerStateRecognized。
和
手势识别器识别离散事件,例如点击或滑动,但不报告手势内的更改。换句话说,离散手势不会通过“开始”和“已更改”状态进行转换,也不会失败或被取消。
UISwipeGestureRecognizer
是一个离散的手势。如果您想要连续(但相似)的手势,请改用UIPanGestureRecognizer
。