我想在摇动设备时截取屏幕截图,并且还希望它在 UIAlertView 可见时起作用。 问题是没有调用 MotionBegan 和 MotionEnded 方法。我已经尝试子类化 UIWindow 和 UIAlertView 并覆盖其中的方法,但仍然没有调用任何方法。
我是不是漏掉了什么?
我通过调用启用摇动手势
UIApplication.SharedApplication.ApplicationSupportsShakeToEdit = true;
在 AppDelegate.cs 中。
当没有 UIAlertView 可见时,一切都完美运行。
当显示 UIAlertView 时,哪个 View 会响应摇动手势?
最佳答案
我强烈建议不要使用 UIAlertView
。
首先它在 iOS9 中被弃用
其次,它真的一团糟
UIView
。 Apple 不支持对其进行子类化,并且在尝试ResignFirstResponder
时事情真的发生了变化,因此您可以接收事件。连同它的许多其他奇怪之处,难怪 Apple 弃用了它。
改用 UIAlertController
,它在 iOS8/9/10 中可用,并且像一个合适的 UIViewController
一样工作,并且在与 UIAlertControllerStyle 一起使用时看起来像旧警报。 Alert
样式,使用时 MotionBegan/MotionEnded
不会有任何问题。
因此您可以在每个 UIViewController
上覆盖 MotionBegan/MotionEnded
:
public override void MotionBegan(UIEventSubtype motion, UIEvent evt)
{
Console.WriteLine("MotionBegin");
base.MotionBegan(motion, evt);
}
public override void MotionEnded(UIEventSubtype motion, UIEvent evt)
{
Console.WriteLine("MotionEnded");
base.MotionEnded(motion, evt);
}
或者创建您自己的 UIWindow 子类,这样您将获得全局运动事件(如果您应用的其他区域需要,您可以通过通知中心发布 (PostNotificationName
)响应运动事件。
public class ShakeWindow : UIWindow
{
public ShakeWindow() : base() { }
public ShakeWindow(IntPtr handle) : base(handle) { }
public override void MotionBegan(UIEventSubtype motion, UIEvent evt)
{
Console.WriteLine("Window MotionBegin");
NSNotificationCenter.DefaultCenter.PostNotificationName("ShakeMe", this);
base.MotionBegan(motion, evt);
}
public override void MotionEnded(UIEventSubtype motion, UIEvent evt)
{
Console.WriteLine("Window MotionEnded");
base.MotionEnded(motion, evt);
}
}
UIAlertController
使用 NSNotificationCenter
的示例:
var button2 = new UIButton(UIButtonType.RoundedRect);
button2.SetTitle("Alert 2", UIControlState.Normal);
button2.Frame = new CGRect(20, 60, 100, 40);
button2.TouchUpInside += (object sender, EventArgs e) =>
{
var alert2 = UIAlertController.Create("StackOverflow", "Shake me now", UIAlertControllerStyle.Alert);
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("ShakeMe"), (obj) =>
{
alert2?.DismissViewController(true, null);
});
alert2.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (UIAlertAction obj) =>
{
alert2.DismissViewController(true, null);
}));
PresentViewControllerAsync(alert2, true);
};
Add(button2);
关于ios - 在显示 UIAlertView 时处理摇动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38497828/