Create Animations in Swift Programming
With Swift, one can create an app with animations. This can be achieved as follows:
Container View Transitions
If you want to perform an animated transition between views, perform this transition in a parent container. In the example below, we are going to perform an animated transition between two UIViews which are colored. The container of the animation will be the third UIView. Let us start by setting up three views in the viewDidLoad() function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
let cner = UIView()
let rSquare = UIView() // red
let bSquare = UIView() //blue
override func viewDidLoad() {
super.viewDidLoad()
// setting the container frame and adding it to the screen
self.cner.frame = CGRect(x: 60, y: 60, width: 200, height: 200)
self.view.addSubview(cner)
// setting up the red frame
// both the red and blue square frames should be located at the same position
// we then just reuse rSquare.frame
self.rSquare.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
self.bSquare.frame = rSquare.frame
// set background colors
self.rSquare.backgroundColor = UIColor.redColor()
self.bSquare.backgroundColor = UIColor.blueColor()
// let us just add the red square for this time. The blue square will be added later in the animation.
self.container.addSubview(self.rSquare)
}
|
Just run the app. A red square will be the output.
Now to achieve the animation between the two squares, let’s create add a @IBAction to the button in the storyboard:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@IBAction func aButtonTapped(sender: AnyObject) {
// create a pair of more objects and assign them to a single variable (tuple)
let vws = (frontView: self.rSquare, backView: self.bSquare)
// set the style of the transition
let tOptions = UIViewAnimationOptions.TransitionCurlUp
UIView.transitionWithView(self.cner, duration: 1.0, options: tOptions, animations: {
// removing the front object…
vws.frontView.removeFromSuperview()
//adding the other object
self.cner.addSubview(vws.backView)
}, completion: { finished in
// once the animation has completed, any code added here will be applied
}
)
}
|
After running the app, you will notice that we will get stuck with the blue square. Let’s solve this by adding some logic code which will check the visible square:
1
2
3
4
5
6
7
8
9
10
11
|
// create a pair of more objects and assign them to a single variable (tuple)
var vws : (frontView: UIView, backView: UIView)
// if rSquare has a superView, example, it is in the container,
// set it as front, and bSquare as back
// otherwise, do flip the order
if(self.rSquare.superview){
vws = (frontView: self.rSquare, backView: self.bSquare)
}
else {
vws = (frontView: self.bSquare, backView: self.rSquare)
}
|
The transition will then be added as follows:
1
2
3
4
5
6
7
8
9
10
11
12
|
@IBAction func aButtonTapped(sender: AnyObject) {
var vws : (frontView: UIView, backView: UIView)
if(self.rSquare.superview){
vws = (frontView: self.rSquare, backView: self.bSquare)
}
else {
vws = (frontView: self.bSquare, backView: self.rSquare)
}
// setting up the transition style
let tOptions = UIViewAnimationOptions.TransitionCurlUp
UIView.transitionFromView(vws.frontView, toView: vws.backView, duration: 1.0, options: tOptions, completion: nil)
}
|
The above will output a unique transition:
The red view will be changed to blue. You can also choose to set the transition to any style of choice such as:
1
|
let tOptions = UIViewAnimationOptions.TransitionFlipFromLeft
|