首页 > 解决方案 > 如何使用 SwiftUI 禁用整个应用程序或仅视图的多点触控?

问题描述

我想在 swiftUI 中禁用多点触控,但我没有找到任何解决方案有没有办法做到这一点???

我发现的一切都与 UIKit 有关:

如何禁用多点触控?

标签: swiftui

解决方案


没有 SwiftUI 解决方案,但您可以在 SwiftUI 应用程序中使用 UIKit 解决方案。

使用UIView.appearance(). 这将禁用整个应用程序的多点触控。

@main
struct SwiftUIDEMO: App {
    init() {
        UIView.appearance().isMultipleTouchEnabled = false
        UIView.appearance().isExclusiveTouch = true
    }
    
    // Body View
}

或者你也可以使用UIViewControllertouch event 和 maange .isUserInteractionEnabled

extension UIViewController {
    open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        self.view.isUserInteractionEnabled = true
    }
    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        self.view.isUserInteractionEnabled = true
    }
    open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        self.view.isUserInteractionEnabled = true
    }
}

推荐阅读