UIFeedback触觉引擎调用次数比激活次数多

时间:2016-10-26 23:49:30

标签: ios swift ios10 uifeedbackgenerator

我正在使用UIFeedback Haptic Engine swift 2.3 ,如:

let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.Warning)

let generator = UIImpactFeedbackGenerator(style: .Heavy)
generator.impactOccurred()

今天我遇到了这样一种新的错误,但是找不到问题。你有什么想法吗?

UIFeedbackHapticEngine _deactivate] called more times than the feedback engine was activated

详细说明:

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x1863e41c0 __exceptionPreprocess
1  libobjc.A.dylib                0x184e1c55c objc_exception_throw
2  CoreFoundation                 0x1863e4094 +[NSException raise:format:]
3  Foundation                     0x186e6e82c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4  UIKit                          0x18cc43fb8 -[_UIFeedbackEngine _deactivate]
5  UIKit                          0x18cad781c -[UIFeedbackGenerator __deactivateWithStyle:]

3 个答案:

答案 0 :(得分:13)

.modal-body { max-height:500px; overflow-y:auto; } 不是线程安全的,因此请确保您同步调用UIImpactFeedbackGenerator而不是generator.impactOccurred()或其他异步线程。

答案 1 :(得分:1)

在{strong> iOS 11 ** 上拨打generator.impactOccurred()会崩溃。您需要在主线程async上调用它。

let generator = UIImpactFeedbackGenerator(style: style)
generator.prepare()

DispatchQueue.main.async {
   generator.impactOccurred()
}

答案 2 :(得分:1)

只需完成一个已经给出的答案:您想要做的就是拥有一个OperationQueue或DispatchQueue,它们将始终用于调用FeedbackGenerator的函数。请记住,对于您的用例,您可能必须释放生成器,但是一个最小的示例是:

class HapticsService {

    private let hapticsQueue = DispatchQueue(label: "dev.alecrim.hapticQueue", qos: .userInteractive)

    typealias FeedbackType = UINotificationFeedbackGenerator.FeedbackType

    private let feedbackGeneator = UINotificationFeedbackGenerator()
    private let selectionGenerator = UISelectionFeedbackGenerator()

    func prepareForHaptic() {
        hapticsQueue.async {
            self.feedbackGeneator.prepare()
            self.selectionGenerator.prepare()
        }
    }

    func performHaptic(feedback: FeedbackType) {
        hapticsQueue.async {
            self.feedbackGeneator.notificationOccurred(feedback)
        }
    }

    func performSelectionHaptic() {
        hapticsQueue.async {
            self.selectionGenerator.selectionChanged()
        }
    }

}

这几乎解决了我们在生产中的相关崩溃。