美文网首页
横竖屏模式判断左右滑动或上下滑动

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

作者: BabyNeedCare | 来源:发表于2023-11-07 11:47 被阅读0次
@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 {
                    // 向左滑动
                }
            }
        }
    }
}

相关文章

网友评论

      本文标题:横竖屏模式判断左右滑动或上下滑动

      本文链接:https://www.haomeiwen.com/subject/jrcfwdtx.html