Swift Basics-Storyboard switching with uiscrollview controls for picture Carousel

Source: Internet
Author: User

Interface switching
In the project, you can put the interface with a high degree of coupling in a StoryBoard. You can use multiple StoryBoards to build interfaces according to functions. This is convenient for project maintenance and multi-person development. For switching between multiple StoryBoards, you can use the following code:


@IBAction func ChangeOne (sender: UIButton) {
        var oneStoryBoard: UIStoryboard = UIStoryboard (name: "One", bundle: NSBundle.mainBundle ())
        let oneController: UIViewController = oneStoryBoard.instantiateViewControllerWithIdentifier ("oneId") as UIViewController
        self.navigationController? .pushViewController (oneController, animated: true)
    }
It should be noted that the above "One" is One.storyboard, and "oneId" is the Storyboard ID in

Another thing to note is that the main interface of the switch interface must be Navigation Contoller



UIScrollView realize picture carousel

//
// Two.swift
// MultStoryBoardChanged
//
// Created by System Administrator on 15/2/6.
// Copyright (c) 2015 jwzhangjie. All rights reserved.
//
import UIKit
class Two: UIViewController, UIScrollViewDelegate {
    
    @IBOutlet weak var scrollview: UIScrollView!
    
    @IBOutlet weak var pageControl: UIPageControl!
    
    var timer: NSTimer!
    
    
    override func viewDidLoad () {
        super.viewDidLoad ();
        // image width
        let imageW: CGFloat = self.scrollview.frame.size.width;
        let imageH: CGFloat = self.scrollview.frame.size.height;
        var imageY: CGFloat = 0;
        var totalCount: NSInteger = 5;
        for index in 0 .. <totalCount {
            var imageView: UIImageView = UIImageView ();
            let imageX: CGFloat = CGFloat (index) * imageW;
            imageView.frame = CGRectMake (imageX, imageY, imageW, imageH);
            let name: NSString = NSString (format: "image_0% d", index + 1);
            imageView.image = UIImage (named: name);
            self.scrollview.showsHorizontalScrollIndicator = false;
            self.scrollview.addSubview (imageView);
        }
        let contentW: CGFloat = imageW * CGFloat (totalCount);
        self.scrollview.contentSize = CGSizeMake (contentW, 0);
        self.scrollview.pagingEnabled = true;
        self.scrollview.delegate = self;

self.pageControl.numberOfPages = totalCount;
        self.addTimer ()
    }
    
    func nextImage (sender: AnyObject!) {
        var page: Int = self.pageControl.currentPage;
        if (page == 4) {
            page = 0;
        } else {
            ++ page;
        }
        let x: CGFloat = CGFloat (page) * self.scrollview.frame.size.width;
        self.scrollview.contentOffset = CGPointMake (x, 0);
    }
    
    func scrollViewDidScroll (scrollView: UIScrollView) {
        let scrollviewW: CGFloat = scrollview.frame.size.width;
        let x: CGFloat = scrollview.contentOffset.x;
        let page: Int = (Int) ((x + scrollviewW / 2) / scrollviewW);
        self.pageControl.currentPage = page;
    }
    
    func scrollViewWillBeginDragging (scrollView: UIScrollView) {
        self.removeTimer ();
    }
    
    
    func scrollViewDidEndDragging (scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.addTimer ();
    }
    
    
    func addTimer () {
        self.timer = NSTimer.scheduledTimerWithTimeInterval (1, target: self, selector: "nextImage:", userInfo: nil, repeats: true);
        NSRunLoop.currentRunLoop (). AddTimer (self.timer, forMode: NSRunLoopCommonModes);
    }
    
    func removeTimer () {
        self.timer.invalidate ();
    }
}


It should be noted that in XCode6.1

for index in 0 .. <totalCount {
0 .. <is a half interval, the header is not the tail ... the entire interval





Swift basics--Switch between StoryBoard and UIScrollView control to realize picture carousel

Related Article

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.