gpt4 book ai didi

ios - 尝试使 UITableViewCell 可滑动和可点击,同时保持 UITableView 可滚动,为什么我的单元格有时只能滑动?

转载 作者:可可西里 更新时间:2023-11-01 06:20:54 29 4
gpt4 key购买 nike

我有一个 UITableView,它的单元格是 UITableViewCell 的子类,它有一个顶部 UIView 和一个底部 UIView,如果用户拖动他/她的手指,允许顶部 UIView 向右和向左移动。

我通过向每个 UITableViewCell 添加一个平移手势识别器来让它工作。很简单的东西。但我只想要水平平移,但它会在它打算向上滚动时检测到整个单元格的垂直平移,这会导致 UITableView 不移动。

我试图做到这一点,只有当用户水平移动他/她的手指超过 5 像素时才会检测到手势,但它似乎不起作用。滚动效果很好,我可以点击单元格,但我在一个单元格上滑动十次,它可能只工作一次。

我不明白为什么。

相关代码:

- (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];
}
}

我究竟应该怎么做才能使这个实现更好地工作?

最佳答案

一个很好的方法是确定手势识别器在哪个方向平移最多并使用它,而不是在移动某个值时才开始平移。它大大减少了您需要的代码量,并且在我的测试中运行良好。

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGPoint translation = [(UIPanGestureRecognizer*)gestureRecognizer translationInView:self.superview];
return fabsf(translation.x) > fabsf(translation.y)
}

return YES;
}

现在您的 View 滑动变得更简单了。此代码允许您向任一方向拖动。

- (void)drag:(UIPanGestureRecognizer *)sender {
CGPoint translation = [sender translationInView:self.superview];
CGRect frame = self.upperView.frame;

switch (sender.state) {
case UIGestureRecognizerStateChanged:
frame.origin.x = translation.x;
[self.upperView setFrame:frame];
break;
case UIGestureRecognizerStateBegan:
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateFailed:
default:
break;
}
}

此外,您可能希望让您的手势识别器与其他手势识别器一起工作(例如,处理表格 View 滚动的识别器),但 gestureRecognizerShouldBegin: 中的代码似乎可以处理此问题没有问题。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

另请注意,我使用的是 self.superview 而不是您使用的 self。这是因为文档的 translationInView: 部分中的这一行:

The view in whose coordinate system the translation of the pan gesture should be computed. If you want to adjust a view's location to keep it under the user's finger, request the translation in that view's superview's coordinate system.

关于ios - 尝试使 UITableViewCell 可滑动和可点击,同时保持 UITableView 可滚动,为什么我的单元格有时只能滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266907/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com