此類(lèi)中的代碼,是已經(jīng)封裝好的,只需要提供dataArray數(shù)組中的圖片就可以實(shí)現(xiàn)無(wú)限輪播
原理: 相同兩張圖片之間快速切換,使人眼無(wú)法分辨出,所以就會(huì)產(chǎn)生輪播的錯(cuò)覺(jué)
demo鏈
接:https://github.com/baiGenZhengliu/Swift-.git
import UIKit
class HJZView: UIView ,UIScrollViewDelegate{
let kScreenW = UIScreen.mainScreen().bounds.size.width
var dataArray:NSArray?;
var _scrollView:UIScrollView?;
//計(jì)時(shí)器
var _timer:NSTimer?;
//主要是為了確定scrollview和pagecontroller的顯示位置
var index:Int64?;
var _pageControl:UIPageControl?;
override init(frame: CGRect) {
super.init(frame: frame);
/*
本代碼只需要改動(dòng)dataArray中的圖片,
pageControl,會(huì)自動(dòng)根據(jù)數(shù)組中的元素個(gè)數(shù)調(diào)整
*/
dataArray = ["aion01.jpg","aion02.jpg","aion03.jpg","aion04.jpg","aion05.jpg"];
//
//創(chuàng)建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?.backgroundColor = UIColor.redColor();
//創(chuàng)建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")
}
//創(chuàng)建scrollview上的圖片控件
func creatImageView(){
index = 0;
var i = 0;
//創(chuàng)建第一張圖片,首個(gè)圖片框
let firstimage = UIImage(named: dataArray?.lastObject as! String)
let firstimageview = UIImageView(frame: CGRect.init(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
firstimageview.image = firstimage
_scrollView?.addSubview(firstimageview)
for imgNmae in dataArray!{
let imgV = UIImageView(frame: CGRectMake(self.bounds.size.width * CGFloat(i+1), 0, self.bounds.size.width, self.bounds.size.height));
imgV.image = UIImage(named: imgNmae as! String);
_scrollView?.addSubview(imgV);
i++ ;
}
//創(chuàng)建最后一個(gè)圖片框
let lastimage = UIImage(named: dataArray?.firstObject as! String)
let lastimageview = UIImageView(frame: CGRect.init(x: self.bounds.size.width * CGFloat((dataArray?.count)! + 1), y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
lastimageview.image = lastimage
_scrollView?.addSubview(lastimageview)
_scrollView?.contentSize = CGSizeMake(self.bounds.size.width * CGFloat((dataArray?.count)! + 2), self.bounds.size.height);
//創(chuàng)建計(jì)時(shí)器
_timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "timeMethod", userInfo: nil, repeats: true);
//創(chuàng)建runloop設(shè)置計(jì)時(shí)器的運(yùn)行模式,保證在滑動(dòng)界面時(shí),計(jì)時(shí)器也在工作
NSRunLoop.currentRunLoop().addTimer(_timer!, forMode: NSRunLoopCommonModes);
}
//計(jì)時(shí)器調(diào)用的方法
func timeMethod(){
index = index!+1;
if NSInteger(index!) == ((dataArray?.count)!) + 1{
index = 0;
_scrollView?.contentOffset = CGPointMake(0, 0)
index = 1;
}
UIView.animateWithDuration(0.3) { () -> Void in
self._scrollView!.contentOffset = CGPointMake(CGFloat((self.index!)) * self.kScreenW, self._scrollView!.contentOffset.y);
};
_pageControl!.currentPage = NSInteger(index!);
if NSInteger(index!) == ((dataArray?.count)!) {
_pageControl!.currentPage = 0;
}
}
// scrollView delegate
func scrollViewDidScroll(scrollView: UIScrollView) {
let x = scrollView.contentOffset.x/kScreenW;
index = Int64(x);
_pageControl!.currentPage = NSInteger(x);
}
}