Swift에서 장치 방향 가져오기
스위프트에서 현재 장치 방향을 어떻게 알 수 있는지 궁금합니다.Objective-C에 대한 예제가 있다는 것은 알고 있지만 Swift에서 작동하지 못했습니다.
장치 방향을 파악하여 if 문에 넣으려고 합니다.
이것이 제가 가장 문제를 겪고 있는 라인입니다.
[[UIApplication sharedApplication] statusBarOrientation]
사용할 수 있는 항목:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
var text=""
switch UIDevice.currentDevice().orientation{
case .Portrait:
text="Portrait"
case .PortraitUpsideDown:
text="PortraitUpsideDown"
case .LandscapeLeft:
text="LandscapeLeft"
case .LandscapeRight:
text="LandscapeRight"
default:
text="Another"
}
NSLog("You have moved: \(text)")
}
SWIFT 3 업데이트
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
var text=""
switch UIDevice.current.orientation{
case .portrait:
text="Portrait"
case .portraitUpsideDown:
text="PortraitUpsideDown"
case .landscapeLeft:
text="LandscapeLeft"
case .landscapeRight:
text="LandscapeRight"
default:
text="Another"
}
NSLog("You have moved: \(text)")
}
또는
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
알림을 통해 확인할 수 있습니다: IOS8 Swift: 방향 변경을 감지하는 방법은 무엇입니까?
참고: 회전 위치인터페이스 방향이 더 이상 사용되지 않습니다. iOS 2.0 이상의 경우 사용 보기WillTransitionToSize
Face up(얼굴을 위로) 및 Face Down(얼굴 아래로)의 경우에는 이 기능이 작동하지 않습니다.그래서 우리는 다음을 사용해야 합니다.
if UIApplication.shared.statusBarOrientation.isLandscape {
// activate landscape changes
} else {
// activate portrait changes
}
Objective-C 코드와 같은 상태 표시줄(및 UI) 방향을 얻으려면 다음과 같이 하십시오.
UIApplication.sharedApplication().statusBarOrientation
UIDevice의 속성을 사용할 수도 있습니다.
UIDevice.currentDevice().orientation
그러나 UI의 방향과 일치하지 않을 수 있습니다.문서에서:
속성 값은 장치의 현재 방향을 나타내는 상수입니다.이 값은 장치의 물리적 방향을 나타내며 응용 프로그램의 사용자 인터페이스의 현재 방향과 다를 수 있습니다.가능한 값에 대한 설명은 "UIDeviceOrientation"을 참조하십시오.
struct DeviceInfo {
struct Orientation {
// indicate current device is in the LandScape orientation
static var isLandscape: Bool {
get {
return UIDevice.current.orientation.isValidInterfaceOrientation
? UIDevice.current.orientation.isLandscape
: UIApplication.shared.statusBarOrientation.isLandscape
}
}
// indicate current device is in the Portrait orientation
static var isPortrait: Bool {
get {
return UIDevice.current.orientation.isValidInterfaceOrientation
? UIDevice.current.orientation.isPortrait
: UIApplication.shared.statusBarOrientation.isPortrait
}
}
}}
swift4 답변: 이것이 제가 하는 방법입니다.
1.모든 종류의 뷰 컨트롤러에서 작동합니다.
2.사용자가 앱을 회전시킬 때도 작동합니다.
3.또한 처음으로 앱을 설치합니다.
애플은 최근 풍경 대 풍경에 대한 아이디어를 없앴습니다.화면 크기를 사용하는 것을 선호하는 것을 선호합니다.그러나 이 방법은 다음과 같습니다.
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("landscape")
} else {
print("portrait")
}
}
스위프트 4:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
print("landscape")
} else {
print("portrait")
}
}
현재 장치 방향을 찾으려면 다음 코드를 사용하십시오.
UIA 응용 프로그램.sharedApplication().statusBarOrientation
신속한 3.0을 위해
UIA 응용 프로그램.shared.statusBarOrientation
statusBarOrientation이 더 이상 사용되지 않으므로 위의 답변과 같이 더 이상 사용할 수 없습니다.
이 코드에서는 감가상각에 대한 걱정 없이 방향을 얻을 수 있습니다.Swift 5 ioS 13.2 100% 테스트 완료
응용 프로그램에서 아래 코드를 사용하려면 세로 및 가로로 작업할 수 있어야 합니다. 그렇지 않으면 결과가 달라집니다.
창문첫번째 창은 주 창입니다. 마지막 창은 현재 창입니다.
struct Orientation {
// indicate current device is in the LandScape orientation
static var isLandscape: Bool {
get {
return UIDevice.current.orientation.isValidInterfaceOrientation
? UIDevice.current.orientation.isLandscape
: (UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isLandscape)!
}
}
// indicate current device is in the Portrait orientation
static var isPortrait: Bool {
get {
return UIDevice.current.orientation.isValidInterfaceOrientation
? UIDevice.current.orientation.isPortrait
: (UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isPortrait)!
}
}
}
사용하는 데 문제가 있었습니다.InterfaceOrientation
로딩 시 방향에 접근하지 않는 것을 제외하고는 정상적으로 작동했습니다.그래서 저는 이것을 시도했고 그것은 키퍼입니다.이것은 효과가 있습니다 왜냐하면bounds.width
는 항상 현재 방향을 참조합니다.nativeBounds.width
그것은 절대적입니다.
if UIScreen.mainScreen().bounds.height > UIScreen.mainScreen().bounds.width {
// do your portrait stuff
} else { // in landscape
// do your landscape stuff
}
나는 이것을 에서 부릅니다.willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)
그리고 부터viewDidLoad
하지만 유연합니다.
해당 방향으로 포인터를 보내준 zSprawl 덕분입니다.이것은 iOS 8 이상에서만 유효하다는 점을 지적해야 합니다.
따라서 애플이 전체 오리엔테이션 문자열("세로", "가로")을 경시한다면, 여러분이 관심을 갖는 것은 너비와 높이의 비율입니다.(@bpedit's answer)
너비를 높이로 나누면 결과가 1보다 작으면 메인 화면이나 용기 또는 "세로" 모드가 됩니다.결과가 1보다 크면 "경관" 그림입니다. ;)
override func viewWillAppear(animated: Bool) {
let size: CGSize = UIScreen.mainScreen().bounds.size
if size.width / size.height > 1 {
print("landscape")
} else {
print("portrait")
}
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if size.width / size.height > 1 {
print("landscape")
} else {
print("portrait")
}
}
(이 접근법을 사용하면 너비와 높이가 동일한 비율이 정확히 1일 때 조건을 처리하는 것에 대해 특별히 신경 쓰지 않을 수 있습니다.)
스위프트 3+
기본적으로:
NotificationCenter.default.addObserver(self, selector: #selector(self.didOrientationChange(_:)), name: .UIDeviceOrientationDidChange, object: nil)
@objc func didOrientationChange(_ notification: Notification) {
//const_pickerBottom.constant = 394
print("other")
switch UIDevice.current.orientation {
case .landscapeLeft, .landscapeRight:
print("landscape")
case .portrait, .portraitUpsideDown:
print("portrait")
default:
print("other")
}
}
:)
Swift 5 – 솔루션:앱 시작 시 및 장치 회전 중에 방향 확인:
// app start
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let orientation = self.view.window?.windowScene?.interfaceOrientation {
let landscape = orientation == .landscapeLeft || orientation == .landscapeRight
}
}
// on rotation
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
let landscape = UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight
}
롭의 대답을 토대로 한 스위프트 3
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if (size.width / size.height > 1) {
print("landscape")
} else {
print("portrait")
}
}
스위프트에서 Obj-C 코드에 대한 대체 코드를 찾았습니다.
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
이라
if UIApplication.shared.statusBarOrientation.isLandscape
참고: 상태 표시줄 방향이 가로인지 여부를 확인하려고 합니다.풍경일 경우 if 문이 참입니다.
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
if (toInterfaceOrientation.isLandscape) {
NSLog("Landscape");
}
else {
NSLog("Portrait");
}
}
스위프트 5
스위프트의 작품UI 및 스토리보드 기반 앱.또한 회전 및 특성 처리기를 확인합니다.
struct Orientation {
/// true - if landscape orientation, false - else
static var isLandscape: Bool {
orientation?.isLandscape ?? window?.windowScene?.interfaceOrientation.isLandscape ?? false
}
/// true - if portrait orientation, false - else
static var isPortrait: Bool {
orientation?.isPortrait ?? (window?.windowScene?.interfaceOrientation.isPortrait ?? false)
}
/// true - if flat orientation, false - else
static var isFlat: Bool {
orientation?.isFlat ?? false
}
/// valid orientation or nil
static var orientation: UIDeviceOrientation? {
UIDevice.current.orientation.isValidInterfaceOrientation ? UIDevice.current.orientation : nil
}
/// Current window (for both SwiftUI and storyboard based app)
static var window: UIWindow? {
guard let scene = UIApplication.shared.connectedScenes.first,
let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let window = windowSceneDelegate.window else {
return UIApplication.shared.windows.first
}
return window
}
}
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
layoutAll()
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
print("viewWillTransition")
layoutAll()
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
print("traitCollectionDidChange")
layoutAll()
}
/// Layout content depending on the orientation
private func layoutAll() {
// Layout as you need
print("layoutAll: isLandscape=\(Orientation.isLandscape), isPortrait=\(Orientation.isPortrait), traitCollection=\(traitCollection)")
}
}
UIDevice.current.orientation은 화면 방향에 관계없이 전화기가 수평일 때 "FaceUp" 또는 "FaceDown"을 보고하기 때문에 나침반 유형 응용 프로그램에서 작동하지 않았습니다.사용자가 자연스러운 방향으로 전화기를 들고 있다고 가정하여 사용자가 향하는 방향을 보여주고 싶습니다.
UIA 응용 프로그램.shared.statusBarOrientation은 "statusBarOrientation"이 iOS 13.0에서 더 이상 사용되지 않음을 경고했습니다. 창 장면의 interfaceOrientation 속성을 대신 사용하십시오."
다음 사람들이 제가 필요로 하는 것을 해주었습니다.
var headingOffset = 0.0
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
switch windowScene?.interfaceOrientation {
case .portrait:
headingOffset = 0.0
print("Reporting orientation as portrait")
case .portraitUpsideDown:
headingOffset = 180.0
print("Reporting orientation as portraitUpsideDown")
case .landscapeLeft:
headingOffset = -90.0
print("Reporting orientation as landscapeLeft")
case .landscapeRight:
headingOffset = 90.0
print("Reporting orientation as landscapeRight")
default:
headingOffset = 0.0
print("Reporting orientation as default")
}
이 과거 iOS 13을 보는 사용자:
저에게 가장 신뢰할 수 있는 방법은 (아직) 작동하고 있지만 지금은 더 이상 사용되지 않습니다.
print(UIApplication.shared.statusBarOrientation.isPortrait)
이제 가야 할 길은 무엇입니까?
if UIApplication.shared.windows.first?.
windowScene?.interfaceOrientation.isPortrait ?? true {
print("Portrait")
} else {
print("Landscape")
}
단순성 유지:
let orientation = UIApplication.shared.statusBarOrientation.isLandscape ? "landscape" : "portrait"
현대의 목표 C와 현재 사용되지 않는 기능을 설명하는 가장 좋은 방법은 다음과 같습니다.
UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]);
신속하게, 그렇게 될 것입니다.
UIDevice.current.orientation.isLandscape
사용해 보십시오.horizontalSizeClass
&verticalSizeClass
:
import SwiftUI
struct DemoView: View {
@Environment(\.horizontalSizeClass) var hSizeClass
@Environment(\.verticalSizeClass) var vSizeClass
var body: some View {
VStack {
if hSizeClass == .compact && vSizeClass == .regular {
VStack {
Text("Vertical View")
}
} else {
HStack {
Text("Horizontal View")
}
}
}
}
}
언급URL : https://stackoverflow.com/questions/25796545/getting-device-orientation-in-swift
'programing' 카테고리의 다른 글
NPM: "npm link" 모듈을 찾을 수 없는 경우 (0) | 2023.08.02 |
---|---|
Angular 6 Unit Tests: All\nReferenceError: 뒤에 오류가 발생했습니다.변수를 찾을 수 없음: $가 던져졌습니다. (0) | 2023.08.02 |
마지막으로 삽입한 Oracle IDENTITY 검색 (0) | 2023.08.02 |
Oracle 시퀀스가 1이 아닌 2로 시작함 (0) | 2023.08.02 |
대형 이미지에서 xxhdpi, xhdpi, hdpi, mdpi 및 ldpi 드로잉을 생성할 수 있는 방법이 있습니까? (0) | 2023.08.02 |