iOS Development: A demo (Swift language Encoding) for an unlimited scrolling auto-play picture

Source: Internet
Author: User

A long time ago wanted to write such an infinite scrolling demo, recently learned the next swift, the hands can not be used to practiced hand the demo, so it is realized.


GitHub address (due to the use of Uiview+autolayout third party layout, GitHub determines that the primary language is OC): Https://github.com/wzpziyi1/DisplayingImage

Using Uicollectionview, unlike the Uiscrollview implementation, it is not necessary to manually implement the cache pool function, because the cell in Uicollectionview is recycled, I just need to deal with infinite scrolling and the removal and addition of timers.

It needs to be noted here that the Mjextension framework does not perfectly support swift, I am writing the process, using it to parse a plist file, did not succeed, and then wrote a KVC parsing, I hope MJ teacher as soon as possible to upgrade it to the swift version of it.

Example Picture:

In the process of writing, the problem is still more, because I am not so familiar with the swift cause, the individual think Swift is the most bad point, is not yet a unified specification. I am from OC to Swift, so the code is basically the set of rules that use OC, but from other languages, there may be a big difference.

    1. This is the code that reads the data from the plist and stores it inside the array:
      Class Zynew:nsobject {    var icon:string!    var title:string!        Init (dict:dictionary<string, string>) {        super.init ()        self.setvaluesforkeyswithdictionary (dict)    }        Class Func Getnews (), array<zynew>    {let        path = Nsbundle.mainbundle (). Pathforresource (" Newses.plist ", Oftype:nil) let        originarray:nsarray? = Nsarray (contentsoffile:path!)        var news = array<zynew> ()        Originarray?. Enumerateobjectsusingblock ({(Obj:anyobject, index:int, stop:unsafemutablepointer<objcbool>), Void in Let            tmp = zynew (dict:obj as! Dictionary<string, string>)            news.append (tmp)        })        return news    }}

      There's a hole in here. The array type in SWIFT does not have a contentsoffile method, that is, an array is not used to read a plist from a file path and then convert it to an array. Well, no that even, anyway can use Nsarray, then I use Nsarray transformation is good, and then fell into the pit.
      In Swift, the nsarray\nsdictionary in OC and so on are all considered to be anyobject types, which means that you are wrong when you convert Nsarray to array<zynew>. For example, in the code above, I use the following code to force conversions:

      Let Originarray:nsarray? = Nsarray (contentsoffile:path!)
      Let news:array<zynew> = Originarray as! [Zynew]
      This turns out to be wrong here, because the real type of anyobject is the dictionary, and then I change it into the code above.

    2. Code for Custom Uicollectionviewcell:
      Import Uikitclass Zynewcell:uicollectionviewcell {//mark:-storage attribute var new:zynew? {didset{//print (new?. icon) Self.imageView.image = UIImage (named: (New?.            ICON)!) Self.titleLabel.text = new?.    Title}}//mark:-Compute properties//mark:-UI Properties private weak Var imageview:uiimageview!                Private weak Var titlelabel:uilabel! Override Init (Frame:cgrect) {super.init (frame:frame) self.commitinit ()} required init? (Coder Adecoder:nscoder) {super.init (Coder:adecoder) Self.commitinit ()} private func Commitinit () {Let Imagev         Iew = Uiimageview () Imageview.contentmode = Uiviewcontentmode.scaleaspectfill Imageview.clipstobounds = True Self.addsubview (imageView) Self.imageview = ImageView Let Titlelabel = UILabel () titl Elabel.textalignment = Nstextalignment.center Titlelabel.textcolor = Uicolor.whitecoLor () Self.addsubview (titlelabel) Self.titlelabel = Titlelabel} override func Layoutsubviews () {        Super.layoutsubviews () self.imageView.autoPinEdgesToSuperviewEdgesWithInsets (Uiedgeinsetszero) Self.titleLabel.autoPinEdgesToSuperviewEdgesWithInsets (Uiedgeinsetszero, ExcludingEdge:ALEdge.Bottom) self.title Label.autosetdimension (Aldimension.height, tosize:30)}}

      It is very well written here, due to the coding habits of OC, I like to layer the properties of the class, so that I can quickly find the problem in the late development, code to see the past is also a glance.

      There are two types of properties in Swift:
      Storage properties: It has Willset and Didset methods
      Computed properties: It has set and get methods
      The feeling is that the getter and setter methods corresponding to the OC can be rewritten.

    3. Main code Zyimagedisplayingview:
      Import Uikitclass Zyimagedisplayingview:uiview, Uicollectionviewdelegate, Uicollectionviewdatasource {//MARK:-Constants Private let identifier = "Zynewcell"//mark:-storage properties override Var frame:cgrect{didset{if (Self.collectionview! = nil) {Self.collectionview?.  Removefromsuperview ()} if (frame.width = = 0.0 && Frame.height = = 0.0 && frame.origin.x = = 0.0 && FRAME.ORIGIN.Y = = 0.0) {return} let L Ayout = Uicollectionviewflowlayout () layout.itemsize = frame.size Layout.scrolldirection = UICollect Ionviewscrolldirection.horizontal layout.minimumlinespacing = 0 Let CollectionView = Uicollectionvie W (frame:cgrectmake (0, 0, Frame.width, frame.height), collectionviewlayout:layout) Collectionview.registerclas S (zynewcell.self, Forcellwithreuseidentifier:identifier) collectionView.showshorizontalscrollindicator = False Self.addsubview (CollectionView) Self.collectionview = Co Llectionview self.collectionview!. delegate = self self.collectionview!. DataSource = self self.collectionview!. BackgroundColor = Uicolor.whitecolor () self.collectionview!. Pagingenabled = True self.collectionview!. Scrolltoitematindexpath (Nsindexpath (foritem:0, INSECTION:ZYIMAGEGROUPS/2), Atscrollposition:            Uicollectionviewscrollposition.none, Animated:false) Self.bringsubviewtofront (Pagecontrol)        Self.addtimer ()}} var news = Zynew.getnews () var timer:nstimer?    Mark:-Compute properties//mark:-UI control weak var collectionview:uicollectionview?        Weak Var pagecontrol:uipagecontrol! Mark:-Initialization method override init (Frame:cgrect) {super.init (frame:frame) self.commitinit ()} requ Ired init? (coder Adecoder:nscoder) {super.init (Coder:adecoder) Self.commitinit ()} private func com Mitinit () {self.backgroundcolor = Uicolor.yellowcolor () var Pagecontrol = Uipagecontrol () Pagecontrol . numberofpages = Self.news.count Pagecontrol.pageindicatortintcolor = Uicolor.redcolor () pagecontrol.current                    Pageindicatortintcolor = Uicolor.whitecolor () self.addsubview (pagecontrol) Self.pagecontrol = PageControl }//mark:-uicollectionviewdatasource func numberofsectionsincollectionview (Collectionview:uic Ollectionview), Int {return zyimagegroups} func CollectionView (Collectionview:uicollectionview, n Umberofitemsinsection section:int), Int {return self.news.count} func CollectionView (CollectionView: Uicollectionview, Cellforitematindexpath Indexpath:nsindexpath), Uicollectionviewcell {var cell:ZYNe Wcell? = CollectionView.Dequeuereusablecellwithreuseidentifier (Self.identifier, Forindexpath:indexpath) as?        Zynewcell if (cell = = nil) {cell = Zynewcell ()}//print (Self.news[indexpath.row]) Cell?.    New = Self.news[indexpath.row] return cell! }//mark:-uiscrollviewdelegate func scrollviewwillbegindragging (scrollview:uiscrollview) {Self.rem         Ovetimer ()}//When ScrollView deceleration is complete, it is best to add timer func scrollviewdidenddecelerating (Scrollview:uiscrollview) at this time Self.addtimer ()} func Scrollviewdidscroll (Scrollview:uiscrollview) {Let size = Scrollview.cont entoffset//print (size) Self.pageControl.currentPage = Int (size.x/(Self.collectionview?. Frame.width)! + 0.5)% Self.news.count}//mark:-Timer handling func AddTimer () {Self.removetimer () Self.timer = Nstimer (Timeinterval:2, Target:self, Selector:selector ("Updatetimer"), Userinfo:nil, repeats:true) NSRunLoop.m AinrUnloop (). AddTimer (self.timer!, Formode:nsrunloopcommonmodes)} func Removetimer () {Self.timer?. Invalidate () Self.timer = nil} func Updatetimer () {Let Currentindexpath = Self.resetindexpath                        () var section = currentindexpath.section var row = Currentindexpath.row + 1 if (row = = Self.news.count) {row = 0 section++} self.collectionview?. Scrolltoitematindexpath (Nsindexpath (Foritem:row, Insection:section), Atscrollposition: Uicollectionviewscrollposition.none, Animated:true)} func Resetindexpath (), Nsindexpath {let C Urrentindexpath = Self.collectionview?. Indexpathsforvisibleitems (). First Self.collectionview?. Scrolltoitematindexpath (Nsindexpath (Foritem): (Currentindexpath?. Row), INSECTION:ZYIMAGEGROUPS/2), AtScrollPosition:UICollectionViewScrollPosition.None, Animated:false) retur N Nsindexpath (foritem: (cuRrentindexpath?. Row), INSECTION:ZYIMAGEGROUPS/2)} override func Layoutsubviews () {super.layoutsubviews () sel F.pagecontrol.autopinedgetosuperviewedge (Aledge.bottom, Withinset:8) Self.pageControl.autoPinEdgeToSuperviewEdge (Aledge.right, withinset:20) self.pageControl.autoSetDimensionsToSize (Cgsizemake (100, 20)}}

        This part of the code is more of a logical process. Plist There are only 5 specific model, I am assuming that Collectionviewcell has 100 groups (that is, section equals 100), each group has 5 rows (that is, row equals 5), every minor scroll to the next cell, I will first let it scroll to Sectio equals 50, and then row does not change, in the beginning to scroll to the next, that is, row+1, if the row is more than the number of model plist, then the corresponding, Section++,row zeroed.

      What is worth saying here is that I rewrote the frame property of the parent class, and when the Zyimagedisplayingview frame changes, it triggers the Didset method of the property, I initialized the Uicollectionview in this method and made the corresponding settings. Personally think this is not very good, I should not be in this method to initialize the Uicollectionview, this should have a few considerations:
              If I use AutoLayout to layout this control, Is the Didset method that does not trigger the frame.
              If I need to change the location of the Zyimagedisplayingview, then to avoid repeating the creation of Uicollectionview,
            & nbsp I must first remove the previously created Uicollectionview, and then
              Create a new CollectionView.

      Other, [Zyimagedisplayingview class] in OC corresponds to zyimagedisplayingview.self in Swift
      OC #pragma mark corresponds to//in Swift MARK:-< br>

    4. code inside the Viewcontroller:

iOS Development: A demo (Swift language Encoding) for an unlimited scrolling auto-play picture

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.