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

Source: Internet
Author: User

Recently started developing a new iOS app and decided to use Swift on its own. A few days later, a very serious problem was found. 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 you find the resources of Swift, look, it's probably half a year ago code. Or a year ago code, one executed. All the errors.

This is because Swift is still developing well, and with the release of Swift2.0, Swift has changed a lot since the announcement of Swift over the years, with a lot of interfaces or grammars that are significantly different before and after. Some features can only be abruptly to look at official documents or crush their brains. is very painful.

Based on the above-mentioned practical reasons, it is also for swift development in the future. I share some of the real-world functions that I realized on Swift to everyone, hoping to reduce the time that we search the Internet. My development environment is OS X 10.10.3. The Xcode release number 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 images with your fingers. Detailed implementations such as the following:

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


(2) Implement the binding of the control to the code. Hold right-drag the control into the code, select outlet, similar to generate code such as the following:

  @IBOutlet weak var galleryscrollview:uiscrollview!   Realize the scrolling of picture carousel;    @IBOutlet weak var gallerypagecontrol:uipagecontrol!  Tip the currently scrolling picture. Indicator.

(3) Drag a few pictures to images.xcassets, this sample drag 5 pictures. The names were gallery1.....gallery5.


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

var timer:nstimer!

(5) To implement the picture scrolling method Picturegallery (), code such as the following:

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 the height of the ScrollView as high as the picture. var imagey:cgfloat = 0;//The y-coordinate of the picture is at the top of the 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, several pictures are placed sequentially from left to right in ScrollView, but scrollview in the interface is just the size of a picture.            effect similar to 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;}//need to be very careful: ScrollThe view control must be set to contentsize, including length and width. Let contentw:cgfloat = Imagew * CGFloat (TotalCount);//The width here is the sum of all the picture widths; self.galleryScrollView.contentSize = Cgsize        Make (CONTENTW, 0);        Self.galleryScrollView.pagingEnabled = true;        Self.galleryScrollView.delegate = self; Self.galleryPageControl.numberOfPages = totalcount;//below the page tip; 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 entrusted 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 events are not running scrolling;    func scrollviewdidscroll (scrollView : Uiscrollview) {            //The code here is run after ScrollView scrolling, not the code that runs ScrollView;            //This is just to set the following pager, 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 final implementation results such as the following:



Hopefully Swift will be getting better. We are relieved to learn swift, the future will certainly shine.


GitHub home: https://github.com/chenyufeng1991.

Welcome to visit us!




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.