In this exercise, we will create an app with several screens that enable us to demonstrate different kinds of animation.
Our app will contain 5 VCs: the main View Controller (Fig.1), a Fade View Controller (Fig. 2), a Slide View Controller (Fig. 3), a Spin View Controller (Fig. 4), and a Door View Controller. Figures 5 and 6 show two different views of the Door View Controller with doors open and closed.
The Main VC is the default View Controller created by Xcode when you create the project. The
code for this VC is in the file ViewController.swift.
Editor -> Embed In… -> Navigation Controller).That's it for now - you don't need to add any code to the file!
FadeViewController.swift. Remember to change the class of the VC in the storyboard
to FadeViewController.labelBazinga.FadeViewController.swift file.
Name the functions onButtonFadeIn and onButtonFadeOut.FadeViewController.swift file for onButtonFadeOut.
@IBAction func onButtonFadeOut(_ sender: Any) {
// Starting alpha value
self.labelBazinga.alpha = 1.0
UIView.animate(
withDuration: 1.0,
animations: {
self.labelBazinga.alpha = 0.0
}
)
}
FadeViewController.Build and run the app to see if everything works.
SlideViewController.swift. Remember to change the class of the VC in the storyboard
to SlideViewController.labelMayTheForce.SlideViewController.swift file. Name the functions
onButtonSlideInFromRight, onButtonSlideInFromLeft,
onButtonSlideOutToRight, and onButtonSlideOutToLeft.SlideViewController.swift file for onButtonSlideInFromRight.
@IBAction func onButtonSlideInFromRight(_ sender: Any) {
// Starting center value
self.labelMayTheForce.center.x = self.view.center.x + self.view.bounds.width
UIView.animate(
withDuration: 3.0,
animations: {
self.labelMayTheForce.center.x -= self.view.bounds.width
}
)
}
SlideViewController.Build and run the app to see if everything works.
SpinViewController.swift. Remember to change the class of the VC in the storyboard
to SpinViewController.labelProsper, labelDuration, and textFieldDuration.onButtonStartSpin function in the SpinViewController.swift file.SpinViewController.swift file for onButtonStartSpin.
@IBAction func onButtonStartSpin(_ sender: Any) {
// Starting transform value
var durationValue = 3.0 // Default duration
if let duration = textfieldDuration.text {
if let durationFloat = Double(duration) {
durationValue = durationFloat
}
}
UIView.animate(
withDuration: durationValue,
animations: {
// 180 degree rotation
self.labelProsper.transform =
self.labelProsper.transform.rotated(by: CGFloat(Double.pi))
}
)
}
SpinViewController.Build and run the app to see if everything works. Try entering different values in the text field and clicking the button.
Assets.xcassets in the File Navigator. Drag and drop the two .png files
directly into the Canvas to create two new assets called leftDoor and
rightDoor.DoorViewController.swift. Remember to change the class of the VC in the storyboard
to DoorViewController.onCloseButtonPressed function in the
DoorViewController.swift file.Image property to "leftDoor". In the Size Inspector, set the values of X, Y, Width,
and Height to 0, 0, 200, and 736 respectively.Image property to "rightDoor". In the Size Inspector, set the values of X, Y, Width,
and Height to 200, 0, 214, and 736 respectively.leftDoor and rightDoor.DoorViewController.swift file for viewDidAppear.
override func viewDidAppear(_ animated: Bool) {
UIView.animate(
withDuration: 0.7,
delay: 1.0,
options: .curveEaseOut,
animations: {
self.leftDoor.frame.origin.x -= self.leftDoor.frame.size.width
self.rightDoor.frame.origin.x += self.rightDoor.frame.size.width
},
completion: { finished in
print("Doors opened!")
})
}
DoorViewController.swift file for onCLoseButtonPressed.
@IBAction func onCloseButtonPressed(_ sender: Any) {
UIView.animate(
withDuration: 0.7,
delay: 1.0,
options: .curveEaseOut,
animations: {
self.leftDoor.frame.origin.x += self.leftDoor.frame.size.width
self.rightDoor.frame.origin.x -= self.rightDoor.frame.size.width
},
completion: { finished in
print("Doors closed!")
})
}
DoorViewController.Build and run the app to see if everything works.
onButtonFadeIn on the FadeViewController to make the
label "Bazinga" disappear, and then fade back in.SlideViewController to make the label slide in
and out on both sides of the screen.