ios - 是否有可能检测到所有手势都发生在一个地方?

标签 ios swift

是否有可能在 iOS 的中心位置检测屏幕上发生的手势,例如,我想是在 AppDelegate 中。我知道我们可以为每个 View 设置手势识别器。但我想在一个中心位置知道所有平移/滚动行为都在发生。因此可以在手势行为发生时设置一些标志。

最佳答案

你可以这样走:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
    window?.addGestureRecognizer(tap)
    return true
 }

 func tapped() {
    print("tapped")
 }

但这并不总是有效,因为您添加的任何交互式 UI 元素都会拦截手势。但是有一个解决方案可以让您始终找到答案:

 (...)   
 let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
 tap.delegate = self
 window?.addGestureRecognizer(tap)
 (...)

然后实现代理的:

 extension AppDelegate: UIGestureRecognizerDelegate {
     func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return true
     }
 }

这样它就不会破坏应用程序的任何正常行为,但您将能够检测手势识别器委托(delegate)方法中的所有触摸。

关于ios - 是否有可能检测到所有手势都发生在一个地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43932349/

相关文章:

ios - 在 UITableViewCell 上设置自定义分隔符

android - React Native - 设置 TextInput 光标位置

ios - 如何对齐 SKPhysicsBody 和 SKShapeNode?

ios - xmppStreamDidConnect 没有被调用

swift - Admob Ads 是如何只出现在某些场景中的?

ios - UIBezierPath 路径的交集

ios - 强、复制、弱和分配原子和非原子的自定义 setter ?

ios - 如何在自动布局 ios 中将 View 的底部边缘和顶部边缘约束到另一个 View ?

ios - 如何从容器 View 中删除 UIViewController?

swift - 将 Swift 泛型类转换为具有类型别名的协议(protocol)