iOS development Project REAL---swift implementation Picture Carousel and browse

Source: Internet
Author: User

Recently started to develop a new iOS app, decided to use Swift, after a few days, found a very serious problem, that is, whether it is a book, or network resources, about Swift is too little, any search is OC to achieve a certain function. Even if it is to find Swift's resources, a look, probably six months ago code, or a year ago code, a run, all error. This is because Swift is still evolving, and with Swift2.0 's open source, including the release of Swift for more than a year, Swift has been a big change, and many interfaces or grammars are significantly different. Some features can only be abruptly to look at official documents or crush the brain to think, very painful.

Based on the above reasons, but also for the future of swift development, I put my own in the swift implementation of some of the real-life functions to share to everyone, hope to reduce the time we search the Internet. My development environment is OS X 10.10.3,xcode release is version 6.4 (6e35b), the project SDK created by default is IOS SDK 8.4.

This time we're going to use Swift to loop through the pictures and swipe left and right to navigate through the pictures with your fingers. The specific implementation is as follows:

(1) Create a swift-based iOS project, open Main.storyboard, drag a scrollview scrolling view in the interface to display the picture, and then place a page control on the ScrollView below Can be used to indicate which picture is currently, interface design


(2) To implement the control and code binding, press and hold right-drag the control into the code, select outlet, similar to the generated code is as follows:

  @IBOutlet weak var galleryscrollview:uiscrollview!   Realize the scrolling of picture carousel;    @IBOutlet weak var gallerypagecontrol:uipagecontrol!  Cue the current scrolling picture, indicator;

(3) Drag a few pictures to images.xcassets, this example drag 5 pictures, the name is Gallery1.....gallery5.


(4) Define a Nstimer type of timer within the class:

var timer:nstimer!

(5) To implement the picture scrolling method Picturegallery (), the code is as follows:

Func picturegallery () {//Implement picture scrolling;//image width let imagew:cgfloat = Self.galleryScrollView.frame.size.wi        dth;//gets the width of the scrollview as the width of the picture; let imageh:cgfloat = self.galleryscrollview.frame.size.height;//gets ScrollView high as a picture; var imagey:cgfloat = 0;//The y-coordinate of the picture is at the top of scrollview; var totalcount:nsinteger = 5;//The number of pictures in the carousel; for index in 0 ..            <totalcount{var imageview:uiimageview = Uiimageview ();            Let imagex:cgfloat = cgfloat (index) * IMAGEW; Imageview.frame = CGRectMake (ImageX, Imagey, Imagew, Imageh);//Set the size of the picture, note the relationship between image and ScrollView, In fact, a few pictures are placed in order from left to right in the ScrollView, but ScrollView display in the interface is only a picture of the size, the effect is similar to the gallery; let name:string = String (format: "Gallery            %d ", index+1);            Imageview.image = UIImage (named:name); Self.galleryScrollView.showsHorizontalScrollIndicator = false;//does not set the horizontal scroll bar; Self.galleryScrollView.addSubview (ima Geview);//Add the picture to the ScrollView to achieve the effect of the carousel;}//You need to be very careful: ScrollVIew control must be set contentsize, including length and width, let contentw:cgfloat = Imagew * CGFloat (TotalCount);//The width here is the sum of all the picture widths; self.ga        Lleryscrollview.contentsize = Cgsizemake (contentw, 0);        Self.galleryScrollView.pagingEnabled = true;        Self.galleryScrollView.delegate = self; self.galleryPageControl.numberOfPages = totalcount;//The page number prompt below; Self.addtimer ()}

(6) To achieve the picture loop playback Method NextImage ():

    Func NextImage (sender:anyobject!) {//Picture carousel;        var page:int = self.galleryPageControl.currentPage;        if (page = = 4) {   //loop;            page = 0;        } else{            page++;        }        Let x:cgfloat = cgfloat (page) * self.galleryScrollView.frame.size.width;        Self.galleryScrollView.contentOffset = Cgpointmake (x, 0);//Note: Contentoffset is the offset of the set ScrollView;    }    

(7) To implement a delegate uiscrollviewdelegate in the current class and implement a Method Scrollviewdidscroll (), the code is as follows:

The overridden method in Uiscrollviewdelegate;    //Handles all ScrollView events after scrolling, noting that the event is not performed on scrolling;    func scrollviewdidscroll (scrollView : Uiscrollview) {            //The code here is performed after ScrollView scrolling, not code to execute ScrollView;            //This is just to set the following page tip, which is done after the picture is scrolled; Let            scrollvieww:cgfloat = galleryScrollView.frame.size.width;            Let x:cgfloat = Galleryscrollview.contentoffset.x;            Let Page:int = (Int) ((x + SCROLLVIEWW/2)/scrollvieww);            self.galleryPageControl.currentPage = page;            }

(8) Set a timer:

    Func AddTimer () {   //Picture-carousel timer;        Self.timer = Nstimer.scheduledtimerwithtimeinterval (5, Target:self, selector : "NextImage:", Userinfo:nil, Repeats:true);    }    

(9) The result of the final implementation is as follows:



Hopefully Swift will be getting better, and we'll be relieved to learn swift, and the future will definitely shine.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS development Project REAL---swift implementation Picture Carousel and browse

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.