How to Rotate and Slide UIView in iOS Swift

Lets start out today’s tutorial on How to Rotate and Slide UIView in iOS Swift.

In this we will learn a little maths too, don’t freak out though after reading math….Just kidding. This is a update to our last tutorial on Custom UIView Shapes. We will start by placing a view on our storyboard and then create its outlet in our controller, change its color to blue and then we will start some coding stuff.

How to Rotate and Slide UIView in iOS Swift
How to Rotate and Slide UIView in iOS Swift

 

We will rotate the view 360° by applying the below code to our view.

In the first line we are rotating the view in z-axis. In next we are giving the angle  M_PI is 180° and we are multiplying it by 2 for making it complete 360°. We call also give it duration and repeat count and same we will apply that animation to the view layer.

We can also create a method if we are using multiple view.

Hit cmd+R and run your project. Yo did it pal..nice (Y). the view centre is the same its not changing and the view it rotating full 360°.

How to Rotate and Slide UIView in iOS Swift
How to Rotate and Slide UIView in iOS Swift

Now Sliding a UIView, its not UIPangesture thought we will work or gesture in couple of days. We will set points of starting and final position and some mid and let the code made the view move.

Lets create a method to slide our view. Initially we will define Initial mid and final positions, then we create the path with that positions our view will move on that defined path. After that we create a CAKeyframeAnimation and apply it on the view.layer.

Write the above function in your controller class and call it when you want to apply the slide on your view. Run your project and see your view sliding on screen.

How to Rotate and Slide UIView in iOS Swift
How to Rotate and Slide UIView in iOS Swift.

Looks good right!! You can customise it according to your requirement. You can download the working project from here.

Today we learn How to Rotate and Slide UIView in iOS Swift 2.1 using Xcode 7.2. We will be back soon with something new and exciting. Till then Happy Coding 😉

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.