我正在使用 Xamarin 开发 iOS 应用程序,我想对 UIScrollView 进行子类化,以便根据其速度处理 ScrollView 中的平移手势。因此,我覆盖了 GestureRecognizerShouldBegin
并检查了平移手势的 VelocityInView
。这对于第一个手势效果很好,但在 ScrollView 运动(减速)时触发的后续平移手势始终报告速度为 (0, 0):
public class MyScroll : UIScrollView
{
public override bool GestureRecognizerShouldBegin(UIGestureRecognizer gestureRecognizer)
{
UIPanGestureRecognizer panGesture = gestureRecognizer as UIPanGestureRecognizer;
if (panGesture != null)
{
CGPoint velocity = panGesture.VelocityInView(this);
Console.WriteLine("Pan gesture velocity: " + velocity);
}
return true;
}
}
滚动运动时平移一次然后第二次后的输出:
Pan gesture velocity: {X=37.92359, Y=-872.2426}
Pan gesture velocity: {X=0, Y=0}
这是一个错误还是预期的行为?
编辑:在 Xamarin 论坛上交叉发布:https://forums.xamarin.com/discussion/54478/uiscrollview-pan-gesture-velocity-reporting-0-if-it-is-already-moving#latest
编辑以澄清:
为了澄清我最终想要做什么:我在水平分页 View 中有一个垂直 ScrollView 。我想检查平移的速度,以便如果平移是“水平”(即 X 速度 > Y 速度),我可以告诉 ScrollView 不处理该手势。默认行为是,一旦 ScrollView 开始运动,另一个手势仍然会滚动,但这使得用户很难水平滚动(跨页面),直到垂直滚动完全稳定。
最佳答案
我终于明白了。感谢@RobertN 的帮助:)
关键是 ScrollView 使用的默认平移手势识别器如果已经处于运动状态,则始终报告 0 速度(例如,先前手势的惯性仍然有效)。添加新的 UIPanGestureRecognizer
是记录后续手势的“实际”速度的好方法,但到那时就已经太晚了,无法影响原始平移手势的 GestureRecognizerShouldBegin
。因此,我所要做的就是向我的新 UIPanGestureRecognizer
添加一个 ShouldBegin
委托(delegate),并使用 that 返回 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);
}
这样,我就让默认的 ScrollView 平移手势识别器完成其工作,而我的新 UIPanGestureRecognizer
可以识别用户何时做出新的水平手势,并传递该手势,以便寻呼机可以寻呼。这使得父寻呼机和垂直页面 ScrollView 的组合可以很好地协同工作(想象一下,即使垂直页面处于运动状态,也可以垂直滚动页面并能够翻阅页面)。请注意,您还需要实现以下方法以允许两个手势识别器同时操作:
[Export("gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:")]
public bool ShouldRecognizeSimultaneously(UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
{
return true;
}
关于ios - 当 ScrollView 移动时,Xamarin UIScrollView GestureRecognizerShouldBegin 报告速度为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33346832/