Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Swift封装的无限轮播头视图

此类中代码,是已经封装好的,在外部创建,给其frame和父视图,即可显示。需要注意的地方,就是图片哪里了,在代码中已经写出来了。就是不给demo链接(其实我demo没上传到git上),自己慢慢看代码吧,哈哈,简书看起来代码,还是挺舒服的。import UIKitclass AmonCirHeadView: UIView ,UIScrollViewDelegate{var dataArray:NSArray?;var _scrollView:UIScrollView?;//计时器var _timer:NSTimer?;//主要是为了确定scrollview和pagecontroller的显示位置var index:Int64?;var _pageControl:UIPageControl?;override init(frame: CGRect) {super.init(frame: frame);//复制全部代码 插入图片,在此更换图片名即可,当然也可以网络请求图片,这个自己去修改逻辑吧dataArray = ["aion01.jpg","aion02.jpg","aion03.jpg"];//创建scrollview_scrollView = UIScrollView(frame: CGRectMake(0, 0, kScreenW,self.bounds.size.height));_scrollView?.bouncesZoom = false;_scrollView?.bounces = false;_scrollView?.showsHorizontalScrollIndicator = false;_scrollView?.showsVerticalScrollIndicator = false;_scrollView?.pagingEnabled = true;_scrollView?.delegate = self;self.addSubview(_scrollView!);_scrollView?.contentSize = CGSizeMake(self.bounds.size.width * CGFloat((dataArray?.count)!), self.bounds.size.height);_scrollView?.backgroundColor = UIColor.redColor();//创建pageControl_pageControl = UIPageControl(frame: CGRectMake(0, self.bounds.size.height - 20, kScreenW, 20));self.addSubview(_pageControl!);_pageControl!.backgroundColor = UIColor.clearColor();_pageControl!.numberOfPages = (dataArray?.count)!;_pageControl!.currentPage = 0;_pageControl!.pageIndicatorTintColor = UIColor.whiteColor();_pageControl!.currentPageIndicatorTintColor = UIColor.orangeColor();creatImageView();}required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}//创建scrollview上的图片控件func creatImageView(){index = 0;var i = 0;for imgNmae in dataArray!{let imgV = UIImageView(frame: CGRectMake(self.bounds.size.width * CGFloat(i), 0, self.bounds.size.width, self.bounds.size.height));imgV.image = UIImage(named: imgNmae as! String);_scrollView?.addSubview(imgV);i = i + 1;}//创建计时器_timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "timeMethod", userInfo: nil, repeats: true);//创建runloop设置计时器的运行模式,保证在滑动界面时,计时器也在工作NSRunLoop.currentRunLoop().addTimer(_timer!, forMode: NSRunLoopCommonModes);}//计时器调用的方法func timeMethod(){index = index!+1;if NSInteger(index!) >= dataArray?.count{index = 0;}UIView.animateWithDuration(0.35) { () -> Void inself._scrollView!.contentOffset = CGPointMake(CGFloat(self.index!) * kScreenW, self._scrollView!.contentOffset.y);};_pageControl!.currentPage = NSInteger(index!);}//scrollview的代理func scrollViewDidScroll(scrollView: UIScrollView) {let x = scrollView.contentOffset.x/kScreenW;index = Int64(x);_pageControl!.currentPage = NSInteger(x);}}下面关于Swift的内容你可能也喜欢:Ubuntu 14.04.4 下安装 Swift 2.2.1  http://www.linuxidc.com/Linux/2016-05/131249.htmUbuntu 15.10安装部署Swift开发环境  http://www.linuxidc.com/Linux/2016-01/126995.htm Swift 的变化:从 2.2 到 3.0 会带来什么  http://www.linuxidc.com/Linux/2015-12/126440.htm Swift 正式开源,同时开源 Swfit 核心库和包管理器 http://www.linuxidc.com/Linux/2015-12/125847.htm Apple Swift学习教程  http://www.linuxidc.com/Linux/2014-09/106420.htm 使用 Swift 构建一个 iOS 的邮件应用 http://www.linuxidc.com/Linux/2014-08/105542.htm Swift 2.0开源化  http://www.linuxidc.com/Linux/2015-06/118594.htm Linux下搭建Swift语言开发学习环境 http://www.linuxidc.com/Linux/2015-12/125983.htm Swift 的详细介绍:请点这里 本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-06/132105.htm