IOS project development practice-declaration and call of translation animation and closure functions for multiple views
In iOS animation, you can set different animation effects for different controls and set different time latencies. Pay attention to the use of closure functions. Next we will implement it.
(1) drag three View Controls of different colors into the Main. storyboard, place them in different locations, and bind them to the code,
.
(3) implement the following in the Code:
Import UIKitclass PositionViewController: UIViewController {@ IBOutlet weak var greenSquare: UIView! @ IBOutlet weak var redSquare: UIView! @ IBOutlet weak var blueSquare: UIView! Override func viewDidLoad () {super. viewDidLoad () // Do any additional setup after loading the view .} override func viewDidAppear (animated: Bool) {// the definition of the closure function; // note that the animations in the animation method is called, and the completion uses the closure function. It can be defined outside, call in, so the code is clearer; func completeGreen (v: Bool) {println (Green Completion)} func completeRed (v: Bool) {println (Red Completion )} func completeBlue (v: Bool) {println (Blue Completion)} func anim Green () {self. greenSquare. center. x = self. view. bounds. width-self. greenSquare. center. x} func animRed () {self. redSquare. center. y = self. view. bounds. height-self. redSquare. center. y} func animBlue () {self. blueSquare. center. y = self. view. bounds. height-self. blueSquare. center. y self. blueSquare. center. x = self. view. bounds. width-self. blueSquare. center. x} // The delay parameter indicates the delay. The first parameter indicates the animation time. // call the closure function. UIV Iew. animateWithDuration (1, delay: 0, options: nil, animations: animGreen, completion: completeGreen) UIView. animateWithDuration (1, delay: 0.5, options: nil, animations: animRed, completion: completeRed) UIView. animateWithDuration (1, delay: 1, options: nil, animations: animBlue, completion: completeBlue)/* parameter prompt: ()-> Void: indicates that the parameter is null, the return value is Void. You must implement this closure function. <# (Bool)-> Void )? # (Bool)-> Void #>: indicates that the parameter is of the Bool type, and the return value is Void? This closure function can be empty. * // UIView. animateWithDuration (<# duration: NSTimeInterval #>, delay: <# NSTimeInterval #>, options: <# UIViewAnimationOptions #>, animations: <# ()-> Void ##() -> Void #>, completion: <# (Bool)-> Void )? # (Bool)-> Void #>)} override func didReceiveMemoryWarning () {super. didreceivemorywarning () // Dispose of any resources that can be recreated .}}
(4) delay is set for three different color blocks. Do not check "Use Auto Layout". The implementation effect is as follows:
.