如何在Flutter游戏中禁用iOS 13+的三指点击/滑动手势?
解决Flutter游戏iOS全局禁用三指编辑手势问题
你之前的操作无效,核心原因是:iOS的三指编辑手势会检查接收触摸事件的最上层UIResponder的editingInteractionConfiguration属性,而Flutter应用中实际处理触摸的是FlutterView,不是你重写的FlutterViewController。
下面是两种可行的解决方案:
方案一:子类化FlutterView并替换
创建自定义FlutterView子类,重写editingInteractionConfiguration属性,再让你的FlutterViewController使用该子类视图:
import Flutter import UIKit class DisableEditGestureFlutterView: FlutterView { @available(iOS 13.0, *) override var editingInteractionConfiguration: UIEditingInteractionConfiguration { .none } } class GameFlutterVC: FlutterViewController { override func loadView() { // 替换为自定义FlutterView let flutterView = DisableEditGestureFlutterView(frame: UIScreen.main.bounds) self.view = flutterView } override func viewWillAppear(_ animated: Bool) { print("This gets printed."); super.viewWillAppear(animated) } }
方案二:全局方法交换(推荐)
如果需要全应用禁用,用Method Swizzling直接修改FlutterView的属性实现,无需逐个修改视图控制器:
import Flutter import UIKit @UIApplicationMain class AppDelegate: FlutterAppDelegate { override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // 全局禁用iOS 13+三指编辑手势 if #available(iOS 13.0, *) { guard let originalMethod = class_getInstanceMethod(FlutterView.self, #selector(getter: UIResponder.editingInteractionConfiguration)), let swizzledMethod = class_getInstanceMethod(AppDelegate.self, #selector(getter: AppDelegate.swizzledEditingConfig)) else { return super.application(application, didFinishLaunchingWithOptions: launchOptions) } method_exchangeImplementations(originalMethod, swizzledMethod) } GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } @available(iOS 13.0, *) @objc private var swizzledEditingConfig: UIEditingInteractionConfiguration { .none } }
补充说明
- 两种方案都确保实际接收触摸的
FlutterView返回.none,系统就不会触发三指编辑相关手势。 - 方案二更适合全局禁用场景,无需修改多个视图控制器。
内容的提问来源于stack exchange,提问作者RobDil
相关产品推荐
相关产品推荐

