gpt4 book ai didi

ios - 当用户向下滑动两次触摸时如何防止 UITableView 中的 ScrollView ?

转载 作者:行者123 更新时间:2023-11-28 19:59:03 24 4
gpt4 key购买 nike

我想要当用户在 UITableView 上向下滑动方向时,我做了一些事情并且表格没有滚动。我该怎么做?

最佳答案

如果您只是想让您的 UISwipeGestureRecognizer 与 UITableView 的 UIPanGestureRecognizer 一起触发,您只需设置滑动手势的委托(delegate)并实现

- (BOOL) gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *) otherGestureRecognizer

然后返回YES

如果您想中断 tableview 的平移手势,您可能无法使用 UISwipeGestureRecognizer 来这样做,因为它在用户滑动一定距离后才会真正触发。

但是,如果需要,您可以使用 UIPanGestureRecognizer 来模拟该行为。我能够得到类似的东西:

#pragma mark UIViewController lifecycle

- (void) viewDidLoad
{
[super viewDidLoad];

UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)];
[panner setDelegate:self];
[panner setMinimumNumberOfTouches:2];
[self.tableView addGestureRecognizer:panner];
}

#pragma mark custom swipe response

- (void) didSwipe
{
NSLog(@"SWIPED!");
}

#pragma mark UIPanGestureRecognizer response

- (void) didPan:(UIPanGestureRecognizer *) recognizer
{
[self cancelGestureRecognizer:self.tableView.panGestureRecognizer];

if (recognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [recognizer translationInView:self.view];

if (translation.y > 20)
{
[self didSwipe];
[self cancelGestureRecognizer:recognizer];
}
}
}

#pragma mark UIGestureRecognzierDelegate implementation
- (BOOL) gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *) otherGestureRecognizer
{
//NOTE: blindly returning yes in this case is usually a bad idea. You should check the recognizers here.
return YES;
}

#pragma mark UIGestureRecognizer stuff that should be in a category and not this file.

- (void) cancelGestureRecognizer:(UIGestureRecognizer *) recognizer
{
if (recognizer.enabled)
{
[recognizer setEnabled:NO];
[recognizer setEnabled:YES];
}
}

就是说,在大多数情况下,仅使用平移手势并根据平移的距离制作任何动画/过渡/交互(即 CGPoint 翻译)会带来更好的用户体验> 在这种情况下)。

关于ios - 当用户向下滑动两次触摸时如何防止 UITableView 中的 ScrollView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24983343/

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