跳转至
Structure

Angle

A geometric angle whose value you access in either radians or degrees.

Declaration

@frozen struct Angle

Overview

Angles are used all over SwiftUI to specify a geometric angle. Common uses include rotation and drawing.

Creating an Angle

You can create an Angle by using either radians or degrees. Each has an initializer as well as a static method:

See usage examples below.

Using an Angle with rotation

Angles are commonly used with the rotationEffect(_:anchor:) and similar modifiers:

struct ContentView: View {
    var body: some View {
        VStack(spacing: 50) {
            Rectangle()
                .frame(width: 100, height: 100)
                .rotationEffect(Angle(degrees: 15))
                .border(Color.orange)
            Rectangle()
                .frame(width: 100, height: 100)
                .rotationEffect(.degrees(15))
                .border(Color.orange)
        }
    }
}
A view containing a VStack with two rectangle, each with a frame width and height of 100, a 15 degree rotation effect, and border color orange, which illustrates the rotation by remaining in the original position; one rectangles rotation effect is defined by the init modifier and the other with the angle modifier.

Using an Angle with drawing

Angles can also be used with drawing. Below, we define a custom Shape called Arc, and make its parameter type Angle. In our View, we pass 270 as the degrees parameter to specify the size of the Arc to be displayed:

struct Arc: Shape {
    var endAngle: Angle

    func path(in rect: CGRect) -> Path {
        Path { p in
            p.addArc(center: CGPoint(x: rect.midX, y: rect.midY),
                     radius: rect.width / 2 ,
                     startAngle: Angle.zero,
                     endAngle: endAngle,
                     clockwise: false)
        }
    }
}

struct ArcView: View {
    var body: some View {
        Arc(endAngle: Angle.degrees(270))
            .stroke(Color.orange, lineWidth: 20)
    }
}
A view containing an arch, which has been defined by a shape struct in the code, with stroke color orange and line width 20; the orange arch completes 270 degrees of a circle, as defined by the parameter type angle and degrees parameter of 270.

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Instance Property

animatableData The data to animate.

degrees The size of the angle, measured in degrees.

hashValue The hash value.

radians The size of the angle, measured in radians.


Type Method

<(lhs:rhs:) Returns a Boolean value indicating whether the value of the first argument is less than that of the second argument.

==(a:b:) Returns a Boolean value indicating whether two values are equal.

degrees(_:) Changes the size of an angle to a specified number of degrees.

radians(_:) Changes the size of an angle to a specified number of radians.


Type Alias

AnimatableData The type defining the data to animate.


Type Property

zero Creates an angle of 0 degrees or 0 radians.


Initializer

init() Creates an angle of 0 degrees or 0 radians.

init(degrees:) Creates an angle from a specified number of degrees.

init(radians:) Creates an angle from a specified number of radians.


Instance Method

hash(into:) Hashes the essential components of this value by feeding them into the given hasher.