![](/img/trans.png)
[英]Using UIPanGestureRecognizer to slide to a new view controller
[英]Making a slide up view similar to the new Google Maps iOS app, troubles with multiple UIPanGestureRecognizer
我正在尝试重现新的iOS Google Maps应用程序的向上滑动菜单的行为。
因此,基本上这是一个视图,您可以向上或向下平移到某个点或向下滑动到某个点。 此视图还可以左右滑动,就像分页滚动视图一样。
它应该上下滚动或滑动到侧面,但不能同时滚动到侧面。
经过大量的研究和测试,我发现有些东西很接近,但并不能像需要它那样工作。
我的实现是一个UIScrollView,具有320x400的帧和960x400的内容视图,具有分页滚动。 然后,我添加一个UIPanGestureRecognizer来处理整个视图的上移。
这是PanRecognizer的handleTouch IBAction:
- (IBAction)handlePan:(UIPanGestureRecognizer *)sender {
NSLog(@"Panning");
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
_firstX = [[sender view] center].x;
_firstY = [[sender view] center].y;
}
if (!IS_IPHONE_5) {
if (_firstY < 216)
translatedPoint = CGPointMake(_firstX, 216);
else
translatedPoint = CGPointMake(_firstX, _firstY+translatedPoint.y);
}
else {
if (_firstY < 307)
translatedPoint = CGPointMake(_firstX, 307);
else
translatedPoint = CGPointMake(_firstX, _firstY+translatedPoint.y);
}
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat velocityY = (0.2*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
CGFloat finalX = _firstX;
CGFloat finalY = translatedPoint.y + velocityY;
if(finalY < _firstY) {
if (IS_IPHONE_5)
finalY = 307;
else
finalY = 216;
}
else if(finalY > _firstY) {
if (IS_IPHONE_5)
finalY = 605;
else
finalY = 515;
}
CGFloat animationDuration = (ABS(velocityY)*.0002)+.2;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidFinish)];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}}
因此,基本上,如果平底锅将视图向上滑动而不是向侧面滑动。 我使用此handlePan方法存在的问题是我无法使其正常工作,因此不允许滚动到一个点以上。 我正在尝试处理的是:
if (_firstY < 216)
translatedPoint = CGPointMake(_firstX, 216);
else
translatedPoint = CGPointMake(_firstX, _firstY+translatedPoint.y);
最大的问题是UIScrollView平移手势识别器与我添加的平移手势同时处理平移。 这意味着如果我也通过将手指也移动到侧面的方式向上滑动视图,则UIScrollView会同时向上和向侧面滑动。
如果我尝试仅滑动到侧面并在执行操作时将视图向上移动1个像素,则平移识别器将结束并完全向上滑动视图,也会发生同样的情况。
我发现的解决方法是将平移识别器设置为要求UIScrollview平移识别器无法工作。
[_panGestureRecognizer requireGestureRecognizerToFail: _slideUpScrollView.panGestureRecognizer];
因此,现在当我向侧面滑动时,它会起作用,并且不会使视图向上移动。 问题的思想是,仅当您松开手指并且视图未滚动到侧面时,它才会将自身设置为失败。 发生的情况是您无法再平移视图,但是如果在其中向上滑动,则将视图滑到“ UIGestureRecognizerStateEnded”。
我查看了UIPanGestureRecognizer主题,并查看了几个选项,包括将平底锅识别器子类化以使其仅识别向上滑动的平底锅,而不识别侧面的平底锅。
你们是否认为我使用的策略正确,还是应该换一种方式看?
理想的解决方案是,当记录到侧面的幻灯片时,它只能水平移动内容并由UIScrollView处理;当记录水平平移时,它只能垂直平移并由我的panreconizer处理。 Safari具有水平和垂直滚动的相同行为。 虽然我不知道如何实现。
感谢您的阅读,让我知道您的想法。 如果许多应用程序都具有出色的向上滑动菜单(例如Google Maps),那就太好了!
编辑:我的目标是iOS 6。
好,我知道了!
我使用了来自UIPanGestureRecognizer的示例-仅垂直或水平限制了我的平移上下滚动至垂直滚动,并且在水平平移时将其转到状态失败。
我还使用了: 限制UIPanGestureRecognizer的移动来阻止视图从平移到远处或远处。
最后,我将requireToFail反转为:
[_slideUpScrollView.panGestureRecognizer requireGestureRecognizerToFail:_panGestureRecognizer];
在那里工作!
如果您有任何疑问或需要改进的地方,请告诉我!
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.