ios UIAlertController,关闭时消除动画?

llew8vvj  于 2023-08-08  发布在  iOS
关注(0)|答案(3)|浏览(142)

这是一张简单的行动表

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是false),它只是点击屏幕,没有俗气的动画。
然而,当用户点击三个选项之一时,或者点击“关闭”时,动作表通过使用俗气的动画离开屏幕。
(In 10.3具体地说,它从屏幕上向下滑动。)
有没有办法关闭退出动画?
x1c 0d1x的数据

如果子类化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)
    }
}


事实上,文本“你应该看到这个”从未出现

wtlkbnrh

wtlkbnrh1#

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

lawou6xi

lawou6xi2#

另一种方法是重写viewController的dismiss方法。如果你不想覆盖其他动画,检查动画标志值或在下面的方法中创建一个标志。
使您的AlertController全局化

var choice = UIAlertController()

字符串
确保将此方法添加到显示警报的viewController中
不使用如下所示的动画解除呈现的警报

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

xcitsw88

xcitsw883#

尝试像这样子类化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)
    }
}

字符串

相关问题