横竖屏模式判断左右滑动或上下滑动

2023-11-07  本文已影响0人  BabyNeedCare
@objc func handleGesture(_ gestureRecognizer: UIGestureRecognizer) {
    if gestureRecognizer.state == .ended {
        let location = gestureRecognizer.location(in: self)
        
        // 获取当前设备的方向
        let deviceOrientation = UIDevice.current.orientation
        
        if deviceOrientation.isPortrait || deviceOrientation.isPortraitUpsideDown {
            // 设备在垂直方向
            if location.x > location.y {
                // 水平滑动
                if gestureRecognizer.location(in: self).x > location.x {
                    // 向右滑动
                } else {
                    // 向左滑动
                }
            } else {
                // 垂直滑动
                if gestureRecognizer.location(in: self).y > location.y {
                    // 向下滑动
                } else {
                    // 向上滑动
                }
            }
        } else if deviceOrientation.isLandscapeLeft || deviceOrientation.isLandscapeRight {
            // 设备在横向方向
            if location.x > location.y {
                // 垂直滑动
                if gestureRecognizer.location(in: self).y > location.y {
                    // 向下滑动
                } else {
                    // 向上滑动
                }
            } else {
                // 水平滑动
                if gestureRecognizer.location(in: self).x > location.x {
                    // 向右滑动
                } else {
                    // 向左滑动
                }
            }
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读