我有一个 UITableView,它的单元格是 UITableViewCell 的子类,它有一个顶部 UIView 和一个底部 UIView,如果用户拖动他/她的手指,顶部 UIView 可以左右移动。
我通过向每个 UITableViewCell 添加一个平移手势识别器来让它工作。很简单的东西。但我只想要水平平移,但是当它打算向上滚动时,它会检测到整个单元格的垂直平移,这会导致 UITableView 不移动。
我试图做到这一点,只有当用户将他/她的手指水平平移超过 5px 时才会检测到手势,但它似乎不起作用。滚动工作正常,我可以点击单元格,但我在一个单元格上滑动十次,它可能工作一次。
我不知道为什么。
相关代码:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)recognizer {
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
NSLog(@"hi");
CGPoint translation = [(UIPanGestureRecognizer *)recognizer translationInView:self];
if (translation.x > 5 || translation.x < -5) {
return YES;
}
else {
return NO;
}
}
else {
return YES;
}
}
- (void)pannedCell:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
_firstTouchPoint = [recognizer translationInView:self];
}
else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint touchPoint = [recognizer translationInView:self];
// Holds the value of how far away from the first touch the finger has moved
CGFloat xPos;
// If the first touch point is left of the current point, it means the user is moving their finger right and the cell must move right
if (_firstTouchPoint.x < touchPoint.x) {
xPos = touchPoint.x - _firstTouchPoint.x;
if (xPos <= 0) {
xPos = 0;
}
}
else {
xPos = -(_firstTouchPoint.x - touchPoint.x);
if (xPos >= 0) {
xPos = 0;
}
}
if (xPos > 10 || xPos < -10) {
// Change our cellFront's origin to the xPos we defined
CGRect frame = self.cellFront.frame;
frame.origin = CGPointMake(xPos, 0);
self.cellFront.frame = frame;
}
}
else if (recognizer.state == UIGestureRecognizerStateEnded) {
[self springBack];
}
else if (recognizer.state == UIGestureRecognizerStateCancelled) {
[self springBack];
}
}
我究竟应该做些什么来使这个实现更好地工作?