ios - 在显示 UIAlertView 时处理摇动手势

标签 ios xamarin xamarin.ios screenshot shake

我想在摇动设备时截取屏幕截图,并且还希望它在 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/

相关文章:

xamarin.ios - 更新后 Xamarin IOS 上的 Servicestack 程序集失败

android - 是否有构建 Xamarin.Forms 项目的最有效方法?

ios - 在 Swift 中获取光标前的字符

ios - Objc 中的 SecRandomCopyBytes 提供程序(SHA1PRNG 或 NativePRNG)类型?

iphone - dismissModalViewControllerAnimated 将原始 View 中的 View 向上移动 20 像素

android - 我怎样才能得到最后插入的事件的ID

iOS - 使用代码或 Storyboard设计器创建 View ?

.net - MonoTouch UITableView 问题

Xamarin Forms AppCenter 崩溃,发送到类的选择器无法识别

ios - 在 UICollectionView 中显示错误的图像