gpt4 book ai didi

ios - UIPanGestureRecognizer 与 ScrollView 冲突

转载 作者:可可西里 更新时间:2023-11-01 03:29:10 25 4
gpt4 key购买 nike

我正在尝试将平移手势识别器添加到包含 ScrollView 的 View 中,但我想我在优先级方面遇到了问题。

我的全局 UIView 有一个 UIPanGestureRecognizer 设置如下:

_bottomPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(bottomPanGestureDetected:)];
_bottomPanGestureRecognizer.minimumNumberOfTouches = 2;
_bottomPanGestureRecognizer.maximumNumberOfTouches = 2;
_bottomPanGestureRecognizer.delaysTouchesBegan = NO;
_bottomPanGestureRecognizer.delaysTouchesEnded = NO;

我想识别这个手势,以从底部向上显示另一个 View 。

问题是 scrollview 在我之前识别了自己的平移手势。

所以我试图推迟它感谢:

[_scrollView.panGestureRecognizer requireGestureRecognizerToFail:_bottomPanGestureRecognizer];

它正在工作,在我的两根手指向下移动到向上识别器后触发 ScrollView 事件,但现在的问题是当我只用一根手指在 ScrollView 中滚动时,滚动会在一小段延迟后起作用。

我想不耽误这次事件,这可能吗?欢迎任何想法!

干杯。

西里尔

最佳答案

如果还没有解决,我帮我解决了。

我将 UIPanGestureRecognizer 添加到 UIScrollView 以检测两个手指平移手势,默认的 UIScrollView 行为(滚动到某处)仍然有效。

所以我所做的是将 UIPanGestureReconizer 添加到 UIScrollView:

UIPanGestureRecognizer *pangestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(displayReloadIndicator:)];
pangestureRecognizer.minimumNumberOfTouches = 2;
pangestureRecognizer.delegate = self;
[self.scrollView addGestureRecognizer:pangestureRecognizer];
[pangestureRecognizer release];

在此之后我添加了代码:

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

return YES;
}

在此之后,我实现了平移手势识别器操作方法。

- (void) displayReloadIndicator:(UIPanGestureRecognizer*) panGestureRecognizer {

UIGestureRecognizerState gestureRecognizerState = gestureRecognizer.state;

CGPoint translation = [gestureRecognizer translationInView:self.scv_bibgesamt];

if (gestureRecognizerState == UIGestureRecognizerStateBegan) {

// create a UIView with all the Pull Refresh Headers and add to UIScrollView
// This is really much lines of code, but its simply creating a UIView (later you'll find a myRefreshHeaderView, which is my base view) and add UIElements e.g. UIActivityIndicatorView, a UILabel and a UIImageView on it
// In iOS 6 you will also have the possibility to add a UIRefreshControl to your UIScrollView

}

else if (gestureRecognizerState == UIGestureRecognizerStateEnded
|| gestureRecognizerState == UIGestureRecognizerStateCancelled) {

if (translation.y >= _myRefreshHeaderView.frame.size.height + 12) { // _myRefreshHeaderView is my baseview

//so the UIScrollView has been dragged down with two fingers over a specific point and have been release now, so we can refresh the content on the UIScrollView
[self refreshContent];

//animatly display the refresh view as the top content of the UIScrollView
[self.scrollView setContentOffset:CGPointMake(0, myRefreshHeaderView.frame.size.height) animated:YES];
}

else {

//the UIScrollView has not been dragged over a specific point so don't do anything (just scroll back to origin)
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

//remove the view (because it's no longer needed)
[_myRefreshHeaderView removeFromSuperview];
}
}

更新:

如果您可能希望从您的导航 Controller 集成向后滑动功能,您应该集成以下代码:

- (void) viewDidLoad {

[super viewDidLoad];


if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}

//setup view controller
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

if (gestureRecognizer == _panGestureRecognizer
&& [self.navigationController.viewControllers count] > 1) {

CGPoint point = [touch locationInView:self.view.window];

if (point.x < 20
|| point.x > self.view.window.frame.size.width - 20) {

return NO;
}
}

return YES;
}

关于ios - UIPanGestureRecognizer 与 ScrollView 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14650991/

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