Here you’ll learn how to move a UIImageView using UIViewAnimation property and other nice stuff in Swift.
We’ve setup a demo project that looks like this:
Let’s start from declaring our UIImageView into ViewController.swift file by ctrl+drag from the image (we’ve called it “myImage”) into the .swift file:
Now let’s see what code is needed for the Move Image blue button (of course we’ve declared it into our .swift file again by ctrl+drag from the button to the code):
We will explain you what happens in the code above line by line:
- IBAction for our Move Image button
- Statement to call UIViewAnimations function, here we set its duration in seconds, the delay for when the animation should start (still in seconds) and the options, which means how our animation should behave like. You may try some test by deleting “.CurveLinear” and type one of the following options: .CurveEaseInOut, .CurveEaseIn and .CurveEaseOut
- Here we set our UIImageView new vertical origin (position on the screen). Please note that the 160 value we have set in our sample project means that our image will stop its animation when its top-left corner point will get to 160 pixels from the top of the screen (which is 0). Also, when writing code into a block statement like UIViewAnimation in Swift, you need to put self. before your view’s name or else XCode will not recognize it.
- This is the Completion block, we can add some other methods that our code will fire as soon as the animation ends
- Print a message on the XCode’s Console that says the animation has ended.
Run the app and tap on Move Image blue button, you’ll see our missile icon moving down to 160 pixels. Pretty cool, right?
All right, let’s now check what Set Alpha = 0 red button does:
Line by line what happens above:
- IBAction for our button
- UIViewAnimation statement, as explained above, please note that here we’ve set 0.5 as delay, so our animation will start 0.5 seconds after tapping the button
- Here we set the opacity of our image to 0.1. Alpha values go from 0.0 to 1.0
- Completion block, again
- Print a message that says the alpha value of our image in the XCode’s Console
Run the app again and tap on Set Alpha = 0 red button, you’ll see that our missile will fade slowly in 0.5 seconds, after 0.5 seconds.
Head over now to the Rotate 90° Clockwise green button, here’s its code:
This time we’ll explain only line 3 (you know the rest already). Here we’ve used CGAffineTransformMakeRotation function to rotate our missile of 90°. Please note that in Swift you have to declare a float value with a CGFloat expression: CGFloat(MY_FLOAT_NUMBER). Also, the common variable for 90° in XCode is M_PI_2, you cannot replace it with a value like 90.0, it won’t work.
Run the code again and tap on the green button, you’ll get a final result like this:
Ok, we’re almost done, now we wanted to implement a last button to reset ourmissile image so bring it back to its original position, alpha and rotation status. Here’s the code for the black Reset Image button:
Line by line what happens in the code above:
- IBAction for our black button
- UIViewAnimation statement
- Set the origin Y of our image back to 20px from the top of the screen
- Set opacity back to 1
- Completion block
- Here we’ve put the rotation function back to 0, which means that the image will return to its original rotation state. Only in this case we can set a numeric value.
Run again the app, play with the colored buttons and then tap the black one, our image will go back to its original state.
Thanks for reading, don't forget to check out our other tutorials!
© 2015 cubycode