Ios 当滚动视图移动时,Xamarin UIScrollView手势识别器应开始报告速度为0
我正在使用Xamarin开发一个iOS应用程序,我想对UIScrollView进行子类化,以便根据其速度处理滚动视图中的平移手势。因此,我覆盖了Ios 当滚动视图移动时,Xamarin UIScrollView手势识别器应开始报告速度为0,ios,xamarin,uiscrollview,xamarin.ios,uigesturerecognizer,Ios,Xamarin,Uiscrollview,Xamarin.ios,Uigesturerecognizer,我正在使用Xamarin开发一个iOS应用程序,我想对UIScrollView进行子类化,以便根据其速度处理滚动视图中的平移手势。因此,我覆盖了手势识别器shouldBegin,并检查了平移手势的速度视图。这对于第一个手势很有效,但在滚动视图运动(减速)时触发的后续平移手势始终报告速度为(0,0): 滚动时平移一次和第二次后的输出: Pan gesture velocity: {X=37.92359, Y=-872.2426} Pan gesture velocity: {X=0, Y=0}
手势识别器shouldBegin
,并检查了平移手势的速度视图。这对于第一个手势很有效,但在滚动视图运动(减速)时触发的后续平移手势始终报告速度为(0,0):
滚动时平移一次和第二次后的输出:
Pan gesture velocity: {X=37.92359, Y=-872.2426}
Pan gesture velocity: {X=0, Y=0}
这是一个bug还是预期的行为
编辑:交叉发布在Xamarin的论坛上:
编辑以澄清:
2015-10-26 12:07:06.676 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-608.4813, Y=0}
2015-10-26 12:07:06.703 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-1213.629, Y=0}
2015-10-26 12:07:06.726 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-935.5507, Y=0}
2015-10-26 12:07:06.771 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:06.772 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:06.772 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:08.882 iOSVelocity[68486:2309184] !!!! ShouldBegin velocity not reset !!!!
2015-10-26 12:07:08.885 iOSVelocity[68486:2309184] GestureRecognizerShouldBegin velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:08.887 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:08.889 iOSVelocity[68486:2309184] gestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.890 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.891 iOSVelocity[68486:2309184] gestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.937 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.938 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.939 iOSVelocity[68486:2309184] gestureRecognizer velocity: {X=-336.9197, Y=0}
2015-10-26 12:07:08.940 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-336.9197, Y=0}
2015-10-26 12:07:08.954 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-650.7258, Y=0}
2015-10-26 12:07:08.961 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-650.7258, Y=0}
2015-10-26 12:07:08.993 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-914.0547, Y=0}
2015-10-26 12:07:09.027 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-734.1516, Y=0}
2015-10-26 12:07:09.032 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-734.1516, Y=0}
2015-10-26 12:07:09.033 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-734.1516, Y=0}
2015-10-26 12:07:09.060 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-1086.368, Y=0}
为了阐明我最终要做的事情:我在水平分页视图中有一个垂直滚动视图。我想检查平移的速度,以便在平移为“水平”(即X速度>Y速度)时告诉滚动视图不要处理该手势。默认行为是,一旦滚动视图处于运动状态,另一个手势仍会滚动,但这使得用户很难水平(跨页面)滚动,直到垂直滚动完全固定
这是一个bug还是预期的行为
就通过GestureRecognitizer上的VelocityInView获取p/s而言,至少根据我的经验,在平移运动开始后,但未停止/重置,应获得0,0。Obj-C/Swift将返回相同的内容,不要问我为什么,必须让实际的iOS开发人员询问原因
如果您真的需要在gestureRecognitizer中开始在您的UIScrollView子类中从任何其他pan识别器中分配一个私有CGPoint,那么在其他任何地方都可以获得velocity,您应该是黄金级的
示例输出:
2015-10-26 12:07:06.676 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-608.4813, Y=0}
2015-10-26 12:07:06.703 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-1213.629, Y=0}
2015-10-26 12:07:06.726 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-935.5507, Y=0}
2015-10-26 12:07:06.771 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:06.772 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:06.772 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:08.882 iOSVelocity[68486:2309184] !!!! ShouldBegin velocity not reset !!!!
2015-10-26 12:07:08.885 iOSVelocity[68486:2309184] GestureRecognizerShouldBegin velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:08.887 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:08.889 iOSVelocity[68486:2309184] gestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.890 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.891 iOSVelocity[68486:2309184] gestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.937 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.938 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.939 iOSVelocity[68486:2309184] gestureRecognizer velocity: {X=-336.9197, Y=0}
2015-10-26 12:07:08.940 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-336.9197, Y=0}
2015-10-26 12:07:08.954 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-650.7258, Y=0}
2015-10-26 12:07:08.961 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-650.7258, Y=0}
2015-10-26 12:07:08.993 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-914.0547, Y=0}
2015-10-26 12:07:09.027 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-734.1516, Y=0}
2015-10-26 12:07:09.032 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-734.1516, Y=0}
2015-10-26 12:07:09.033 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-734.1516, Y=0}
2015-10-26 12:07:09.060 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-1086.368, Y=0}
UIScrollView子类示例:
2015-10-26 12:07:06.676 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-608.4813, Y=0}
2015-10-26 12:07:06.703 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-1213.629, Y=0}
2015-10-26 12:07:06.726 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-935.5507, Y=0}
2015-10-26 12:07:06.771 iOSVelocity[68486:2309184] Touch-enabed Pan gesture velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:06.772 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:06.772 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:08.882 iOSVelocity[68486:2309184] !!!! ShouldBegin velocity not reset !!!!
2015-10-26 12:07:08.885 iOSVelocity[68486:2309184] GestureRecognizerShouldBegin velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:08.887 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-1191.385, Y=-8.564461}
2015-10-26 12:07:08.889 iOSVelocity[68486:2309184] gestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.890 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.891 iOSVelocity[68486:2309184] gestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.937 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.938 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=0, Y=0}
2015-10-26 12:07:08.939 iOSVelocity[68486:2309184] gestureRecognizer velocity: {X=-336.9197, Y=0}
2015-10-26 12:07:08.940 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-336.9197, Y=0}
2015-10-26 12:07:08.954 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-650.7258, Y=0}
2015-10-26 12:07:08.961 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-650.7258, Y=0}
2015-10-26 12:07:08.993 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-914.0547, Y=0}
2015-10-26 12:07:09.027 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-734.1516, Y=0}
2015-10-26 12:07:09.032 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-734.1516, Y=0}
2015-10-26 12:07:09.033 iOSVelocity[68486:2309184] otherGestureRecognizer velocity: {X=-734.1516, Y=0}
2015-10-26 12:07:09.060 iOSVelocity[68486:2309184] Touch-enabled Pan gesture velocity: {X=-1086.368, Y=0}
注意:这将使用应与手势识别器同时识别
,以便在用户提起触摸后继续自动平移
注2:不确定是否捕获了所有手势状态排列,因此根据需要进行调整
我终于明白了。感谢@RobertN的帮助:)
关键在于,滚动视图使用的默认平移手势识别器如果已经处于运动状态,则始终报告0速度(例如,前一个手势的惯性仍然有效)。添加新的UIPangestureRecognitizer
是记录后续手势的“实际”速度的好方法,但到那时,影响原始平移手势的手势识别器就太晚了。因此,我所要做的就是添加一个ShouldBegin
委托给我的新UIPangestureRecognitizer
,并使用该委托返回false
,以防我希望手势“掉到”父页面
public MyScroll() : base()
{
UIPanGestureRecognizer panGesture = new UIPanGestureRecognizer();
panGesture.ShouldBegin = delegate(UIGestureRecognizer recognizer)
{
CGPoint v = panGesture.VelocityInView(this);
if (v.X != 0 || v.Y != 0)
{
if (Math.Abs(v.X) > Math.Abs(v.Y))
{
return false;
}
}
return true;
};
this.AddGestureRecognizer(panGesture);
}
这样,我就可以让默认的滚动视图平移手势识别器完成它的工作,而我的新uipangestureerecognizer
可以识别用户何时做出一个新的水平手势,并将该手势传递给用户,以便寻呼机可以翻页。这使得父分页器和垂直页面滚动视图的组合可以很好地协同工作(想象一下,即使垂直页面处于运动状态,也可以垂直滚动页面并能够在页面间翻转)。注意,您还需要实现以下方法以允许两个手势识别器同时操作:
[Export("gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:")]
public bool ShouldRecognizeSimultaneously(UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
{
return true;
}
我尝试了这一点,但似乎发生的是,当您第二次做手势(移动时),默认的平移手势识别器(报告0)调用GestureRecognitizerShouldBegin
,然后使用的速度是“新”平移手势识别器最后报告的速度。换言之,新的平移手势保留了前一个手势的过时值,因此在“常规”平移手势失败时(报告0),新的平移手势尚未“开始”并更新其值。我不知道如何让自定义的方法启动ShouldBegin
方法…我刚刚还尝试从滚动视图中删除默认的平移手势识别器,只剩下新的平移手势识别器。我看到它进入“开始”状态的消息,但只有将该手势指定给滚动视图时,滚动视图才不会触发手势识别器shouldBegin
方法,也不会再移动。将这个手势“连接”到我认为的视图是错误的。我不认为我做了什么特别的事情,这是整个UIScrollView子类,我将它放在一个视图(AddSubView)上,并添加了一个非常大的图像(UIImage通过UIImageView)以便可以平移。现在,我通常不会再和velocitys打交道了…,当用户的体验改变时,他们似乎总是皱起眉头。。。关于使用手势识别器,您的用例是什么?也许还有另一种方法可以做你需要的(?)来澄清我最终要做的事情:我在水平分页视图中有一个垂直滚动视图。我想检查平移的速度,以便在平移为“水平”(即X速度>Y速度)时告诉滚动视图不要处理该手势。默认行为是,一旦滚动视图处于运动状态,另一个手势仍会滚动,但这使得用户很难水平滚动(跨页面),直到垂直滚动完全固定。我准确地复制了您的代码,只是它提供了一个“旧”速度。仅将日志输出保留在手势识别器shouldBegin
方法中,然后向下滚动然后向上滚动。您会注意到,初始平移的Y速度为负(向下):gestureRecognitizerShouldBegin速度:{X=73.03223,Y=-985.935}
,然后第二个手势显示其位置的“零格”