How to convert Degrees to Radians in Swift

⋅ 2 min read ⋅ Swift

Table of Contents

In this article, we will learn different ways to convert Degrees to Radians in iOS.

What is Radian

There are two different units used to measure an angle

  1. Radians
  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.

  1. Create a function
  2. Create an extension
  3. Use the Angle type.
  4. Use the Measurement struct

Function to convert Degrees to Radians in iOS

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

  • A complete rotation angle in radians unit is 2π Radians.
  • A complete rotation angle in degrees unit is 360 Degrees.

So, 1 Degree = 2π Radians/360 = π Radians/180

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 SwiftUI

let 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)

Read more article about Swift or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

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.

Become a patron Buy me a coffee Tweet Share
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