Swift Learning--The simple use of closures (iii)

Source: Internet
Author: User

I. Simple use of closures in swift

 Overridefunc viewdidload () {super.viewdidload ()/** Closures and blocks in OC are very similar to blocks in OC similar to anonymous function closures are used to define function functions: Blocks are used to hold a point, and closures are used to hold a point when needed To perform a time-consuming operation when needed*/        /** Basic format of closures: {(formal parameter list)---() in code to execute}*/        /** * Several formats of closures: 1. Pass the closure through the arguments to the function 2. If the closure is the last parameter of the function, then the closure can be written at the back of the function () 3. If the function receives only one parameter and the parameter is closed Package, then () you can omit*/LoadData (Ten, finished: {(), ()inchPrint ("It's been recalled.")}) LoadData (Ten) {(), ()inchPrint ("It's been recalled.")} say {()()inchPrint ("Hello")        }        /** Shorthand for closures: If the closure has no parameters and no return value, then everything in the previous can be deleted, including in*/LoadData (Ten) {print ("HAHFHAHF")}}} Func say (finish: ()-()) {} func LoadData (Num:int, Finished: ()-()) {print ("perform time-consuming operations")        //Callback Notification callerfinished ()}OverrideFunc Touchesbegan (touches:set<uitouch>, witheventEvent: Uievent?) {        //the Dispatch_async callback in Swift is a closed packetDispatch_async (Dispatch_get_global_queue (0,0)) {(), Voidinchprint (Nsthread.currentthread ()) Print ("perform time-consuming operations") Dispatch_async (Dispatch_get_main_queue (), {()-Voidinchprint (Nsthread.currentthread ()) Print ("back to the main thread update UI")            })        }    }

Two. Create a uiscrollview using closures

 Overridefunc viewdidload () {super.viewdidload () let SC= Createscrollview ({), Intinch            return 5}) {(index)UIViewinchLet width= theLet btn=UIButton ()//3. Setting the properties of the buttonBtn.backgroundcolor =Uicolor.greencolor () btn.settitle ("title \ (index)", ForState:UIControlState.Normal) btn.frame= CGRect (X:index * width, y:0, Width:width, Height: -)                //4. Return to the built-in controls                returnbtn} view.addsubview (SC)}//define a method to create a uiscrollview,//1. And how many buttons on the Uiscrollview must be told by the closure of the method//2. Buttons are also created by closing packagesFunc Createscrollview (Btncount: (), Int, Btnwithindex: (index:int)->uiview),Uiscrollview {//1. Create UiscrollviewLet sc = Uiscrollview (frame:cgrect (x:0Y: -, Width:375, Height: -)) Sc.backgroundcolor=Uicolor.redcolor ()//Let width =Let count =Btncount ()//Add multiple buttons         forIinch 0.. <Count {//Let btn = UIButton ()//            //set the properties of a button//Btn.backgroundcolor = Uicolor.greencolor ()//btn.settitle ("title \ (i)", ForState:UIControlState.Normal)//btn.frame = CGRect (x:i * width, y:0, width:width, height:44)//            //Add a button//Sc.addsubview (BTN)Let SubView =Btnwithindex (index:i) Sc.addsubview (subView) sc.contentsize= Cgsize (Width:cgfloat (count) * subView.bounds.width, Height: -)        }                        returnSC}

Swift Learning--The simple use of closures (c)

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.