ios - 在关闭动画之前运行 UIAlertController 完成处理程序 block

标签 ios swift cocoa-touch uialertcontroller

我正在使用 UIAlertController 设置一个非常标准的操作表:

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

    alert.addAction(
        UIAlertAction(title: "button",
                      style: .default,
                      handler: { _ in
                        MediaPlayer.playSound(.click)
                        // The above line just uses AVAudioPlayer to play
                        // an MP3 file. Its implementation isn't relevant
                        // to the question and can be assumed to be correct.
    }))

当我按下按钮时,警报消失,然后运行完成处理程序并播放声音。由于警报的关闭动画需要一段时间,因此在我按下按钮和播放声音之间存在非常明显的延迟;当声音开始时,警报甚至不再可见。

有没有办法一按下按钮就播放声音,而不是等到关闭动画完成?

此外,有没有简单的方法来做到这一点的原因? IE。 Apple 是否反对人们在他们的提醒按钮上添加点击声音?

最佳答案

对此您基本上无能为力。一旦 UIAlertController 替换了 UIAlertView,用户点击和按钮的操作处理程序运行之间的延迟就很明显了:

UIAlertController dismiss is slow

实际上,您正在尝试做一些 Apple 不希望您做的事情,即将声音与警报 Controller 按钮相关联。

我对这类事情的解决方案很简单:不要使用 UIAlertController。构建一个看起来只是像 UIAlertController View 的自定义呈现 View Controller 非常容易,实际上 UIAlertController 呈现 View Controller 。所以就自己动手吧。现在按钮是你的了,你可以检测到点击并用声音响应然后关闭,没问题。

关于ios - 在关闭动画之前运行 UIAlertController 完成处理程序 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49683251/

相关文章:

ios - 当键盘出现在 SwiftUI 中时向上移动 TextField

ios - SwiftUI 应用程序可以在预览版中运行,但不能在模拟器中运行

swift - macOS:从 CharacterPalette 获取表情符号(修订版)

objective-c - 通过 UITableView 从 NSMutableDictionary 添加/删除项目

objective-c - AnimationDidStop 未被调用

ios - 删除所选 UIButton 中的突出显示

ios - 使用 CLLocationManager 获取 iBeacon 邻近更新

ios - 带有 CollectionView 和文本行的 TableView 部分

cocoa-touch - 在UITextView中显示带有超大首字母的文本 block

ios - 如何使用segues创建一个uiview,它被storyboard中的多个UIViewControllers使用