iOS 기본 (SwiftUI)
[iOS - SwiftUI] Angle, rotationGesture, rotationEffect 사용 방법 (회전 방법, pinch 제스쳐)
jake-kim
2022. 10. 16. 23:29
Angle
- SwiftUI 내부적으로 정의한 구조체
- Angle은 반지름(radians)와 각도 degrees를 가지고 있는 형태
- Angle은 뒤에서 알아볼 rotationGesture, rotationEffect와 같은 곳에서 사용
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@frozen public struct Angle {
public var radians: Double
@inlinable public var degrees: Double
@inlinable public init()
@inlinable public init(radians: Double)
@inlinable public init(degrees: Double)
@inlinable public static func radians(_ radians: Double) -> Angle
@inlinable public static func degrees(_ degrees: Double) -> Angle
}
rotationEffect
- rotationEffect 함수를 호출하여 사용
@inlinable public func rotationEffect(_ angle: Angle, anchor: UnitPoint = .center) -> some View
- ex) 글자를 20도만큼 회전
struct ContentView: View {
@State private var angle: Angle = .init(degrees: 20)
var body: some View {
Text("Jake의 iOS 앱 개발 알아가기")
.foregroundColor(.blue)
.frame(width: 200, height: 200)
.rotationEffect(angle)
}
}
rotationGesture
- .gesture()안에 RotationGesture() 인스턴스를 넣어서 사용
struct ContentView: View {
@State private var angle: Angle = .init(degrees: 20)
var body: some View {
Text("Jake의 iOS 앱 개발 알아가기")
.foregroundColor(.blue)
.rotationEffect(angle)
.gesture(rotationGesture)
}
private var rotationGesture: some Gesture {
RotationGesture()
.onChanged {
print($0)
angle = $0
}
}
}
* 전체 코드: https://github.com/JK0369/ExRotationGesture-SwiftUI
* 참고
https://developer.apple.com/documentation/swiftui/rotationgesture
https://developer.apple.com/documentation/swiftui/view/rotationeffect(_:anchor:)