此類中代碼,是已經封裝好的,在外部創建,給其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 http://www.linuxidc.com/Linux/2016-05/131249.htm
Ubuntu 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 的詳細介紹:請點這裡