IOS project development practice-declaration and call of translation animation and closure functions for multiple views

Source: Internet
Author: User

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:

 

.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.