问题
I'm working on a custom swipe event for a UITableView that uses custom UITableViewCell subclass. I included the UIGestureRecognizerDelegate in my header, and have this in viewDidLoad:
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.numberOfTouchesRequired = 1;
[self.tableView addGestureRecognizer:swipeLeft];
My swipeLeft method looks like so:
-(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!");
}
}
It's sort of working, but the swipe has to be incredibly precise. Like nearly impossibly precise.
I almost got it working by using a UIPanGestureRecognizer instead, but unfortunately it didn't play nice with the global side drawer component that uses a global pan gesture recognizer (ECSlidingViewController for those interested).
Is there any way around this? Any help would be appreciated, as I've been googling around and browsing SO for hours looking for a solution.
回答1:
As pointed out by Kolin Krewinkel on Twitter, implementing these 2 delegate methods did the trick:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
回答2:
I was having a similar problem using the ECSlidingViewController and swipe-to-delete on a UITableView (the top view controller in my case slides to the left to reveal the menu).
I fixed the problem by adding a delegate to the default panGesture property of my ECSlidingViewController like this to only pull in the menu if the swipe starts in the very right-hand edge of the screen:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer locationInView:gestureRecognizer.view].x > ([UIScreen mainScreen].bounds.size.width - 60.0))
{
return YES;
}
return NO;
}
来源:https://stackoverflow.com/questions/15890305/uitableview-swipe-gesture-requires-near-perfect-accuracy