我正在为使用自定义UITableViewCell子类的UITableView开发自定义滑动事件。我在标题中添加了UIGestureRecognizerDelegate
,并在viewDidLoad:
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.numberOfTouchesRequired = 1;
[self.tableView addGestureRecognizer:swipeLeft];
我的swipeLeft方法如下所示:
-(void)didSwipe:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateEnded)
{
CGPoint swipeLocation = [recognizer locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
NSDictionary *clip = [self.clips objectAtIndex:swipedIndexPath.row];
NSLog(@"Swiped!");
}
}
它的排序工作,但滑动必须非常精确。像几乎不可能精确。
我差点通过使用UIPanGestureRecognizer来实现它,但不幸的是,它与使用全局平移手势识别器(ECSlidingViewController,感兴趣的人)的全局侧抽屉组件不太搭配。
这有什么办法吗?任何帮助将不胜感激,因为我一直在谷歌搜索并浏览了几个小时寻找解决方案。
答案 0 :(得分:8)
正如Twitter上的Kolin Krewinkel指出的那样,实现这两种委托方法就可以解决问题:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
答案 1 :(得分:0)
我在使用ECSlidingViewController时遇到了类似的问题,并在UITableView上滑动删除(在我的情况下,顶视图控制器向左滑动以显示菜单)。
我通过向我的panGesture
的默认ECSlidingViewController
属性添加委托来修复此问题,如果滑动在屏幕的右边缘开始,则仅拉入菜单:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer locationInView:gestureRecognizer.view].x > ([UIScreen mainScreen].bounds.size.width - 60.0))
{
return YES;
}
return NO;
}