IOS project development practice-achieve view switching Animation
Switching between different interfaces or views is one of the most common dynamic effects of applications. No matter which platform is used for project development, default view switching is often monotonous, without any animation, the interface switching is also very abrupt. If you use animated effects to make the interface active, your App will be very dynamic. The animation effect during the view switching process will be implemented here. The specific implementation is as follows:
(1) In this experiment, two images will be dragged into the code instead of being directly placed in the View Controller. The directory structure after being dragged to Main. storyboard is as follows:
.
(2) bind the image to the Code Outlet:
@IBOutlet weak var image1: UIImageView! @IBOutlet weak var image2: UIImageView!
(3) implement the following code in ViewController:
Import UIKitclass ViewController: UIViewController {@ IBOutlet weak var image1: UIImageView! @ IBOutlet weak var image2: UIImageView! Var isFirstPic: Bool = true // determine whether the image is the first one; enable two images to switch cyclically; override func viewDidLoad () {super. viewDidLoad () self. view. addSubview (image1) // first load the first image;} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .} override func touchesBegan (touches: Set
, WithEvent event: UIEvent) {func complete (v: Bool) {// The operation executed after each view switch; println (Already Complete) isFirstPic =! IsFirstPic} if (isFirstPic) {// switch the first image to the second image; UIView. transitionFromView (image1, toView: image2, duration: 1, options: UIViewAnimationOptions. transitionFlipFromLeft, completion: complete)} else {// switch the second image to the first image; UIView. transitionFromView (image2, toView: image1, duration: 1, options: UIViewAnimationOptions. transitionFlipFromTop, completion: complete )}}}
(4) Note that UIViewAnimationOptions is the option to switch the animation. You can see multiple options in it. There are 8 animation effects in total: You can try it by yourself. There is always one that suits you.
.