xcode UITableView 滑动以删除 iOS 上的手势冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25526171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gestures Conflict on UITableView swipe to delete iOS
提问by Alvin John Tandoc
I have a problem in my gesture recognizers. My goal is to implement using swipe to delete in my table view. But I think other gestures are conflicting with each other. I'm using this libray romaonthego/REFrostedViewController this a library for my hamburger menu and this library has a pangesture feature. I think the conflict is w/in the gestures. because when I run my code of my tableview in another project it's working.Pls help, Thank you in advance.
我的手势识别器有问题。我的目标是在我的表视图中使用滑动来删除。但我认为其他手势是相互冲突的。我正在使用这个库romaonthego/REFrostedViewController 这是我的汉堡菜单库,这个库有一个 pangesture 功能。我认为冲突在于手势。因为当我在另一个项目中运行我的 tableview 代码时,它正在工作。请帮忙,在此先感谢您。
回答by Kamen Dobrev
I had a similar problem, what I ended up doing is similar to TonyMkenu , but there are more recognizers that you need to allow:
我有一个类似的问题,我最终做的是类似于 TonyMkenu ,但你需要允许更多的识别器:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (otherGestureRecognizer.delegate == self )
return NO;
//if otherGestureRecognizer is swipe to delete from a UITableView cancel slide menu recognizers
if ([[otherGestureRecognizer.view class] isSubclassOfClass:[UITableView class]])
{
NSLog(@"Allow1 %@", [otherGestureRecognizer description]);
return YES;
}
if( [[otherGestureRecognizer.view class] isSubclassOfClass:[UITableViewCell class]] ||
[NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:@"UITableViewCellScrollView"] ||
[NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:@"UITableViewWrapperView"])
{
NSLog(@"Allow&Disable %@", [otherGestureRecognizer description]);
if(gestureRecognizer.delegate == self)
{//cancel the slide menu recognizer
gestureRecognizer.enabled = NO;
gestureRecognizer.enabled = YES;
}
return YES;
}
NSLog(@"Deny %@", [otherGestureRecognizer description]);
return NO;
}
}
回答by beebcon
Edit: Updated for iOS 11
编辑:针对 iOS 11 更新
The other answers were helpful, but in my case the best solution was to do the logic in shouldRequireFailureOfOtherGesture
like so:
其他答案很有帮助,但就我而言,最好的解决方案是按照如下方式执行逻辑shouldRequireFailureOfOtherGesture
:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer == self.pan) {
return YES;
}
return NO;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer == self.pan) {
// iOS 10
if ([NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:@"UITableViewWrapperView"]) {
return YES;
}
// iOS 11
else if ([otherGestureRecognizer isMemberOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer.view isKindOfClass:[UITableView class]]) {
return YES;
}
}
return NO;
}
This had a much better behavior in my case. I also used delaysTouchesBegan = YES
on my pan gesture. Might be useful!
在我的情况下,这有更好的行为。我也用delaysTouchesBegan = YES
在我的平移手势上。可能有用!
回答by Calabash Ball
In iOS 11,hope this can help you.
在 iOS 11 中,希望这可以帮助您。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([[otherGestureRecognizer.view class] isSubclassOfClass:[UITableView class]]) {
if ([otherGestureRecognizer isKindOfClass: [UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer *otherPan = (UIPanGestureRecognizer *)otherGestureRecognizer;
CGPoint translation = [otherPan translationInView:otherGestureRecognizer.view];
return translation.x < 0;
}
}
return NO;
}
回答by TonyMkenu
First of all... check if you have this
首先......检查你是否有这个
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
and second...
其次...
try to add this
尝试添加这个
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([gestureRecognizer.view isKindOfClass:[UITableView class]]) {
return YES;
} else {
return NO;
}
}