today I studied the swift language, casually looking for a small demo, a little research on the essence of Swift grammar, the overall feeling is very comfortable, is to understand the grammar of the waste of some time, I feel that the swift language is difficult to understand at the beginning, but after understanding, you can really appreciate the language of convenience, Not too much of a burden, the language is really a good language, nonsense not much to say, record their own understanding notes, if there is anything wrong, we have a lot of advice ....
This is the Uiviewcontroller implementation code
//Import FrameImport UIKit//The entrance to the file, where the main editing program isclassViewcontroller:uiviewcontroller {Overridefunc viewdidload () {//calling the parent load methodsuper.viewdidload ()//Setting the Frame,let of the carousel is a constant in swift, which is not the same as the OC constants, which are marked with constant amount, let can store object typeLet frame = CGRectMake (0, -, View.bounds.width, view.bounds.width*0.6) //This let is an immutable groupLet ImageView = ["2.jpg","3.jpg","4.jpg"] //creating a view in Swift is simple, just calling the view method to pass parametersLet Loopview = Adloopview (Frame:frame, Images:imageview, AutoPlay:true, Delay:3, Isfromnet:false) //Add agent has not changedLoopview.Delegate= Self//Add View method, Basic and OC no changesView.addsubview (Loopview)/*from these we can see that Swift's language maintains the OC syntax naming rules, for OC development Siege Lion is a gospel, call method is very simple method name (parameter parameters can be)*/ }}//This is the controller that follows the Proxy method keyword in extension, the role of extension is to add an extension to this file, even if the extension in OCextension viewcontroller:adloopviewdelegate{//method naming rules, this is the proxy method, the proxy method naming rules and methods and OC difference is not muchfunc Adloopview (Adloopview:adloopview, Iconclick index:nsinteger) {print (index)}}
View-Implemented Files
Privatevar currentimages:nsmutablearray?{ //get method for Namutablearray Get{currentimgs.removeallobjects ()//Let constant! Indicates that there must be a value in the images arrayLet count = self.images!. count var i= Nsinteger (self.currentpage! + count-1)%Count Currentimgs.addobject (self.images![i]) currentimgs.addobject (self.images! [self.currentpage!]) I= Nsinteger (self.currentpage!+1) %Count Currentimgs.addobject (self.images![i])//return value returnCurrentimgs}} //The private decoration of this variable can only use var for this file to indicate that the variable value can be changed? indicates that this variable can be null or has a value Privatevar images:nsarray?//BOOL type change is not yes/no but True/false Privatevar autoplay:bool?Privatevar isfromnet:bool?Privatevar delay:nstimeinterval?//Create proxy variable var delegate: protocol name?VarDelegate: Adloopviewdelegate?//override flag with parent init method prefix OverrideInit (frame:cgrect) {super.init (frame:frame)}
Required init?(coder Adecoder:nscoder) {fatalerror ("Init (coder:) has not been implemented") } //Convenience Modifying the init function of this classconvenience init (frame:cgrect, Images:nsarray, Autoplay:bool, Delay:nstimeinterval, Isfromnet:bool) {sel F.init (frame:frame) self.images=Images Self.autoplay=AutoPlay self.isfromnet=isfromnet Self.delay=Delay Self.currentpage=0Createimagescrollview () Createpageview ( )//if judgment and Co's syntax format has not changed, relative to OC or simplified, in swift there is no continuation OC 0 fake 1 true ifImages.count <2{Self.autoplay=falsePagecontrol?. Hidden =true } ifSelf.autoplay = =true{Startautoplay ()}}
//this for condition 0: <3 means 0 to 3 of which includes 0 1 2 not including 3 forIndexinch 0.. <3{Let ImageView= Uiimageview (Frame:cgrectmake (self.bounds.width*cgfloat (index),0, Self.bounds.width, Self.bounds.height)) Imageview.userinteractionenabled=trueImageview.addgesturerecognizer (UITapGestureRecognizer (target:self,action: #selector (ADLOOPVIEW.IMAGEVIEWCL Ick )))ifSelf.isfromnet = =true { }Else{imageview.image= UIImage (named:self.currentimages![ Index as!String); } Imagescrollview?. Addsubview (ImageView)}
// as! says weak as is a strong transition. as! String);
//Extended Fulfillment Uiscrollviewdelegate Methodextension adloopview:uiscrollviewdelegate{//Uiscrollview Proxy Methodfunc scrollviewdidenddecelerating (scrollview:uiscrollview) {Scrollview.setcontentoffset (CGPointMa Ke (self.frame.width,0), Animated:true) } //Uiscrollview Proxy Methodfunc Scrollviewdidscroll (scrollview:uiscrollview) {Let x=scrollview.contentoffset.x Let width=Self.frame.widthifX >=2*Width {currentpage= (currentpage!+1)% self.images!. Count Pagecontrol!. CurrentPage = currentpage!refreshimages ()}ifX <=0{currentpage= (currentpage!+self.images!. count-1)% self.images!. Count Pagecontrol!. CurrentPage = currentpage!refreshimages ()}} }
// the custom proxy method prefix uses the @objc protocol proxy name: and follows the protocol {proxy method}@objc protocol Adloopviewdelegate:nsobjectprotocol { func Adloopview (Adloopview:adloopview, Iconclick index:nsinteger) }
Let's get here today ... I'll learn it sometime.
Swift Initial contact Basic syntax method naming (a)