There are two different units used to measure an angle

2. Degrees

I think degrees are easier to understand for most people, including me, but the fact that radian is heavily used in physics and engineering makes it more popular in calculation-related API.

That's why you will see most APIs in iOS use radians instead of degrees.

Here is an example of an API to create a rotation transform. The API accepts an angle in radians.

``let view = UIView()view.transform = CGAffineTransform(rotationAngle: .pi)``

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

How to convert Degrees to Radians in iOS

There are many ways to convert Degrees to Radians. I will show you different ways to do it in iOS.

Function to convert Degrees to Radians in iOS

We can easily convert between degrees and radians using simple math.

• A complete rotation angle in degrees unit is 360 Degrees.

We can create a Swift helper function like this:

``func degreesToRadians(_ degrees: Double) -> Double {    return degrees * .pi / 180}``

We can use this function to pass degree to a function that accepts radians like this:

``let view = UIView()view.transform = CGAffineTransform(rotationAngle: degreesToRadians(180))``

Extension to convert Degrees to Radians in iOS

Instead of a function, you can create an extension for this.

``extension Double {    var radiansUnit: Double {        return self * .pi / 180    }}``

Convert Degrees to Radians using SwiftUI

SwiftUI has a dedicated `Angle` type.

`Angle` type allows us to:

• Initialize by either Degress and Radians.
• Getting a value in either Degress and Radians unit.

I think this is the perfect type for this job.

But the `Angle` struct is defined under the SwiftUI module, so you must import the `SwiftUI` module before using it.

``import SwiftUIlet angleInDegrees = Angle(degrees: 180)let angleInRadians = angleInDegrees.radians``

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Convert Degrees to Radians using Measurement structure

Apple also got a `Measurement` struct, which is a struct used to describe a unit of measure.

It supports a wide range of units, and angle (`UnitAngle`) is one of them.

The `Measurement` struct has built-in methods for calculation and conversion.

Here is how you use it.

``let degrees = Measurement(value: 180, unit: UnitAngle.degrees)let radians = degrees.converted(to: .radians)``

Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Previous
How to fix "libobjc.A.dylib is being read from process memory" error

If your app takes forever to run on a device and you get this error in the console log. I will share what works for me in this article.

Next
Different ways to map over Dictionary in Swift

Learn how to map function work with Swift dictionary.

← Home