如何使用SwiftUI在整个应用程序或仅一个视图上禁用多点触摸?

如何使用SwiftUI在整个应用程序或仅一个视图上禁用多点触摸?,swiftui,Swiftui,我想在swiftUI中禁用多点触摸,但我找不到任何解决方案。有什么方法可以做到这一点吗 我发现的一切都与UIKit有关: 没有SwiftUI解决方案,但您可以在SwiftUI应用程序中使用UIKit解决方案 使用UIView.appearance()。这将禁用整个应用程序上的多点触摸 @main struct SwiftUIDEMO: App { init() { UIView.appearance().isMultipleTouchEnabled = false

我想在swiftUI中禁用多点触摸,但我找不到任何解决方案。有什么方法可以做到这一点吗

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


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

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

@main
struct SwiftUIDEMO: App {
    init() {
        UIView.appearance().isMultipleTouchEnabled = false
        UIView.appearance().isExclusiveTouch = true
    }
    
    // Body View
}
或者您也可以使用
UIViewController
touch 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
    }
}
扩展UIViewController{ 打开覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){ super.touchesMoved(touches,with:event) self.view.isUserInteractionEnabled=true } 打开覆盖函数touchesend(touch:Set,带有事件:UIEvent?){ super.touchesend(触摸,带有:事件) self.view.isUserInteractionEnabled=true } open override func touchesCancelled(Touchs:Set,带有事件:UIEvent?){ super.touchesend(触摸,带有:事件) self.view.isUserInteractionEnabled=true } }
如果您使用的是first code init,那么您可以将此init代码写入您的SwiftUI视图,对于第二种方法,只需创建一个文件并放置代码您的解决方案是wright tnx,但我知道我的问题是其他问题,我应该禁用多拖动手势而不是触摸