码迷,mamicode.com
首页 > 编程语言 > 详细

Swift基础 - - StoryBoard间切换与UIScrollView控件实现图片轮播

时间:2015-02-06 16:53:53      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:swift 基础语法

界面切换

在项目中可以把耦合度比较高的界面放在通过一个StoryBoard中,可以按照功能使用多个StoryBoard搭建界面,这样便于项目维护以及多人开发,对于多个StoryBoard间切换,可以使用以下代码:

@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)
    }
需要说明的是上面的"One"是One.storyboard,"oneId"是下图中的Storyboard ID
技术分享

还有一个需要注意的就是切换界面的主界面必须是Navigation Contoller

技术分享

UIScrollView实现图片轮播

// // 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(); } }

技术分享

需要注意的是XCode6.1里面

for index in 0..<totalCount{
0..<是半区间,包头不包尾    ...全区间



Swift基础 - - StoryBoard间切换与UIScrollView控件实现图片轮播

标签:swift 基础语法

原文地址:http://blog.csdn.net/jwzhangjie/article/details/43564263

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!