我正在使用Monotouch(5.0.4)为Ipad开发应用程序。我有滑动手势的问题,我希望有人可以帮助我,滑动手势会调用我的目标代表两次。
具体案例如下: 在我的UINavigationController中,我在重写的方法ViewDidLoad中推送第一个UIViewController(它是根)。在此视图控制器中,我注册一个滑动手势(左),在其目标委托中推送导航控制器中的新视图控制器。这也适用于不同视图控制器的多次推送。但是,在按下第一个视图控制器后,右滑动手势(弹出视图控制器)会调用其委托两次,并弹出两个视图控制器。这不会发生在根视图控制器中。
我在这个简单的课程中对问题做了一个独立的测试。它的作用是为左右滑动注册两个滑动识别器。左侧滑动推动同一视图控制器的新实例,右滑动弹出它。我也尝试使用选择器而不是NSAction委托,结果相同。
public class SwipeTest : UIViewController
{
public override void LoadView ()
{
base.LoadView ();
// Create left swipe recognizer
UISwipeGestureRecognizer leftRecognizer = new UISwipeGestureRecognizer ();
leftRecognizer.Direction = UISwipeGestureRecognizerDirection.Left;
leftRecognizer.Delegate = new UIGestureRecognizerDelegate ();
leftRecognizer.AddTarget (new NSAction (OnSwipeLeft));
View.AddGestureRecognizer (leftRecognizer);
// Create right swipe recognizer
UISwipeGestureRecognizer rightRecognizer = new UISwipeGestureRecognizer ();
rightRecognizer.Direction = UISwipeGestureRecognizerDirection.Right;
rightRecognizer.Delegate = new UIGestureRecognizerDelegate ();
rightRecognizer.AddTarget (new NSAction (OnSwipeRight));
View.AddGestureRecognizer (rightRecognizer);
}
public override void ViewWillDisappear (bool animated)
{
base.ViewWillDisappear (animated);
}
private void OnSwipeLeft ()
{
Console.WriteLine ("swipe left");
NavigationManager.Instance.PushViewController (new SwipeTest (), true);
}
private void OnSwipeRight ()
{
Console.WriteLine ("swipe right");
NavigationManager.Instance.PopViewControllerAnimated (true);
}
}
NavigationManager是我的UINavigationController,它是一个单例,但没有做太多其他事情。
public class NavigationManager : UINavigationController
{
#region Singleton
class Nested
{
static Nested () {}
internal static readonly NavigationManager instance = new NavigationManager();
}
public static NavigationManager Instance
{
get { return Nested.instance; }
}
#endregion
#region Private fields
private UIViewController firstController;
private UIViewController root;
#endregion
#region Constructor
private NavigationManager ()
{
// Set first view
//firstController = new InitializationView ();
firstController = new SwipeTest ();
}
#endregion
#region Methods
public override void ViewDidLoad ()
{
PushViewController (firstController, false);
base.ViewDidLoad ();
}
#endregion
#region Properties
public UIViewController RootController
{
get {
return root;
}
set {
root = value;
ViewControllers = new UIViewController[1] {value};
}
}
#endregion
}
所以,回顾一下:左滑动(推动视图控制器)工作,右滑动(弹出视图控制器)第一次在根控制器上工作(所以它实际上没有做任何事情),但在视图之后控制器已被推送(在根控制器之后),右滑动调用代理两次。
我希望有人知道为什么会发生这种情况,我已经用我对Monotouch的了解来试试。
答案 0 :(得分:2)
你可能早就想到了这一点,但是我提出了一个方便的Swipe扩展方法。
在LoadView中调用:
View.Swipe(UISwipeGestureRecognizerDirection.Right).Event += (s, r) =>
new UIAlertView("Swipe Test", "Swipe " + r.Direction.ToString(), null, "Ok")
.Show();