此类中代码,是已经封装好的,在外部创建,给其frame和父视图,即可显示。需要注意的地方,就是图片哪里了,在代码中已经写出来了。
就是不给demo链接(其实我demo没上传到git上),自己慢慢看代码吧,哈哈,简书看起来代码,还是挺舒服的。
import UIKit
class 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 in
self._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
Swift 正式开源,同时开源 Swfit 核心库和包管理器
Swift 的详细介绍:请点这里