Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - SwiftUI] Angle, rotationGesture, rotationEffect 사용 방법 (회전 방법, pinch 제스쳐) 본문

iOS 기본 (SwiftUI)

[iOS - SwiftUI] Angle, rotationGesture, rotationEffect 사용 방법 (회전 방법, pinch 제스쳐)

jake-kim 2022. 10. 16. 23:29

목차) SwiftUI의 기본 - 목차 링크

Angle

https://developer.apple.com/documentation/swiftui/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

https://developer.apple.com/documentation/swiftui/view/rotationeffect(_:anchor:)

  • 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

https://developer.apple.com/documentation/swiftui/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:) 

https://developer.apple.com/documentation/swiftui/angle

Comments