【问题标题】:UIAlertController, eliminate the animation on closing?UIAlertController,消除关闭动画?
【发布时间】:2021-08-31 16:34:34
【问题描述】:

这是一个简单的操作表,

let choice = UIAlertController(title: "Choose", message: nil, preferredStyle: .actionSheet)

choice.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.happyCamera() }))

choice.addAction(UIAlertAction(title: "Album", style: .default, handler: { _ in
        self.happyAlbum() }))

choice.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

somewhere?.present(choice, animated: false, completion: nil)

当动作表出现时(注意 present#animated 为假),它只是点击屏幕,没有俗气的动画。

但是,当用户点击三个选项中的一个,或者点击“关闭”时,操作表会通过使用俗气的动画离开屏幕。

(特别是在 10.3 中,它会从屏幕上向下滑动。)

有没有办法关闭退出动画?


如果你继承 UIAlertController...它不起作用?

正如 DS 建议的那样,您可以继承 UIAlertController。

然而——奇怪的是——它什么也没做。这是一个测试

func _test() {
    
    let msg = SuperiorUIAlertController(
        title: "Hello", message: "Hello",
        preferredStyle: UIAlertControllerStyle.alert)
    msg.addAction(UIAlertAction(
        title: "OK", style: UIAlertActionStyle.default,
        handler: nil))
    
    let win = UIWindow(frame: UIScreen.main.bounds)
    let vc = UIViewController()
    vc.view.backgroundColor = .clear
    win.rootViewController = vc
    win.windowLevel = UIWindowLevelAlert + 1
    win.makeKeyAndVisible()    
    vc.present(msg, animated: false, completion: nil)
}

class SuperiorUIAlertController: UIAlertController {
    
    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
        
        print("You should see this! \(flag)")
        self.dismiss(animated: false, completion: completion)
    }
}

确实,“你应该看到这个”的文字从未出现

【问题讨论】:

  • 在处理程序中添加dismiss
  • 你被设置为 false 的动画为 Present 而不是 Dismiss。
  • @Fattie 查看我的更新答案

标签: ios uialertcontroller


【解决方案1】:

我不想回答自己的问题,但截至 2017 年底,没有办法。很奇怪吧?

希望这个事实对某人有所帮助。

【讨论】:

  • 我也有同样的烦恼——你有没有机会向苹果提交错误报告?如果没有,我正在考虑这样做。
  • 荒谬吧? @A.L.Strin 你知道,我只是没有打扰 - 苹果通常不会修复错误 5-10 年,所以为什么要打扰:/
【解决方案2】:

您可以覆盖 viewController 的解除方法的另一种方法。如果您不想覆盖其他动画,请检查动画标志值或在下面的方法中制作标志。

使您的 AlertController 全局化

var choice = UIAlertController()   

确保在你的 viewController 中添加这个方法,你会看到警报

在没有动画的情况下关闭显示的警报,如下所示

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    self.choice.dismiss(animated: false, completion: nil)
}

【讨论】:

  • dismiss(animated:completion:) 是 UIViewController 的实例方法之一。检查它developer.apple.com/documentation/uikit/uiviewcontroller/…
  • 是的。你可以覆盖它的一种方法。我已经在我的回答中指出了。
  • 奇怪的是,这不起作用。实际上,甚至没有调用覆盖:/
  • 确实dismissin the presenting view controller被调用了,我一直觉得很奇怪。
  • 我很欣赏这个作品,@DSDharma,谢谢。但是它会闪烁,因为您正在中断警报本身的解除,以及灰色背景。无论如何,谢谢,谢谢。
【解决方案3】:

尝试像这样子类化 UIAlertController:

class InstantCloseAlertController: UIAlertController {
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UIView.setAnimationsEnabled(false)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        UIView.setAnimationsEnabled(true)
    }
}

【讨论】:

    猜你喜欢
    • 2020-04-28
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多