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

@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 {
                    // 向左滑动
                }
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容