这是一个错误还是预期的行为?
就通过 GestureRecognizerShouldBegin 上的 VelocityInView 获取 p/s 而言,在平移运动开始后获得 0,0,但不会停止/重置,这是预期的,至少根据我的经验。 Obj-C/Swift 会返回同样的东西,不要问我为什么,必须让一个真正的 iOS 开发者来问那个原因。
如果您真的需要在 GestureRecognizerShouldBegin 内从任何其他平移识别器(我在下面的示例中执行此操作)在您的 UIScrollView 子类中分配一个私有 CGPoint,那么在“任何地方”获取速度,您应该是黄金级的。 .
示例输出:
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 子类示例:
注意:这使用了shouldRecognizeSimultaneouslyWithGestureRecognizer,以便在用户解除触摸后继续自动平移
注意 2:不确定我是否捕获了所有手势状态排列,因此请根据需要进行调整
using System;
using UIKit;
using CoreGraphics;
using CoreFoundation;
using CoreData;
using Foundation;
using CoreMotion;
namespace iOSVelocity
{
public class MyScroll : UIScrollView
{
UIPanGestureRecognizer panGesture;
CGPoint velocity;
public MyScroll (CGRect cGRect) : base (cGRect)
{
panGesture = new UIPanGestureRecognizer (() => {
if ((panGesture.State == UIGestureRecognizerState.Began || panGesture.State == UIGestureRecognizerState.Changed) && (panGesture.NumberOfTouches == 1)) {
velocity = panGesture.VelocityInView (this);
Console.WriteLine ("Touch-enabled Pan gesture velocity: " + velocity);
} else if (panGesture.State == UIGestureRecognizerState.Ended) {
// Gesture ended, but auto-panning could still be going...
}
});
AddGestureRecognizer (panGesture);
}
[Export ("gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:")]
public bool ShouldRecognizeSimultaneously (UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
{
if (gestureRecognizer is UIPanGestureRecognizer) {
var panRecognizer = (UIPanGestureRecognizer)gestureRecognizer;
velocity = panRecognizer.VelocityInView (this);
Console.WriteLine ("gestureRecognizer velocity: " + velocity);
} else if (otherGestureRecognizer is UIPanGestureRecognizer) {
var panRecognizer2 = (UIPanGestureRecognizer)otherGestureRecognizer;
CGPoint beginvelocity = panRecognizer2.VelocityInView(this);
if (beginvelocity.X != 0 && beginvelocity.Y != 0)
velocity = panRecognizer2.VelocityInView (this);
Console.WriteLine ("otherGestureRecognizer velocity: " + velocity);
} else {
// What should we do here?
}
return true;
}
public override bool GestureRecognizerShouldBegin (UIGestureRecognizer gestureRecognizer)
{
UIPanGestureRecognizer panGesture = gestureRecognizer as UIPanGestureRecognizer;
if (panGesture != null) {
CGPoint beginvelocity = panGesture.VelocityInView(this);
if (beginvelocity.X == 0 && beginvelocity.Y == 0) {
Console.WriteLine ("!!!! ShouldBegin velocity not reset !!!!");
} else {
velocity = beginvelocity;
}
Console.WriteLine ("GestureRecognizerShouldBegin velocity: " + velocity);
}
return true;
}
}
}