從今天開(kāi)始拜秧,我學(xué)習(xí)的重點(diǎn)開(kāi)始轉(zhuǎn)向Swift萍恕,并且會(huì)分享一些自己學(xué)習(xí)的心得體會(huì),今天給大家?guī)?lái)的的是無(wú)限輪播蟀拷。廣告頁(yè)的無(wú)限輪播是非常常見(jiàn)的一個(gè)功能碰纬,大多數(shù)APP都有,大多數(shù)程序員也都實(shí)現(xiàn)過(guò)问芬,今天我們用Swift實(shí)現(xiàn)一下悦析。項(xiàng)目地址
??圖片切換我們可以選擇的基本控件有兩個(gè)UIScrollView 和 UICollectionView,這次我們選擇UICollectionView此衅;既然是輪播强戴,就會(huì)用到Timer。所以挡鞍,我們這次主要應(yīng)用的知識(shí)點(diǎn)為UICollectionView 和 Timer骑歹;
import UIKit
class CycleScrollView: UIView, UICollectionViewDelegate,UICollectionViewDataSource {
var bottomView : UICollectionView?
var width : CGFloat?
var height : CGFloat?
var timer : Timer?
override init(frame: CGRect){
super.init(frame: frame)
// 1.設(shè)置背景色
self.backgroundColor = UIColor.clear
// 2.設(shè)置寬高
width = self.frame.size.width
height = self.frame.size.height
// 3.添加bottomView
setupBottomView()
// 4.添加定時(shí)器
setupTimer()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupBottomView() {
// 5.設(shè)置collectionView的布局
let flowLayout = UICollectionViewFlowLayout();
flowLayout.itemSize = self.bounds.size
flowLayout.minimumLineSpacing = 0;
flowLayout.minimumInteritemSpacing = 0;
flowLayout.scrollDirection = UICollectionViewScrollDirection.horizontal;
bottomView = UICollectionView.init(frame: self.bounds, collectionViewLayout: flowLayout)
self.addSubview(bottomView!);
// 6.設(shè)置collectionView的尺寸
bottomView?.contentSize = CGSize(width:width! * CGFloat(4),height:height!)
// 7.分頁(yè)
bottomView?.isPagingEnabled = true
// 8.去掉滾動(dòng)條
bottomView?.showsVerticalScrollIndicator = false
bottomView?.showsHorizontalScrollIndicator = false
// 9.設(shè)置代理
bottomView?.delegate = self
bottomView?.dataSource = self
// 10.注冊(cè)cell
bottomView?.register(UICollectionViewCell().classForCoder, forCellWithReuseIdentifier: "ID");
if #available(iOS 10.0, *) {
// 11.預(yù)加載
bottomView?.isPrefetchingEnabled = true
} else {
// Fallback on earlier versions
}
}
func setupTimer() {
// 12.實(shí)例化定時(shí)器
timer = Timer.init(timeInterval: 2, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true);
RunLoop.main.add(timer!, forMode: RunLoopMode.defaultRunLoopMode);
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.timer?.fire();
}
}
func timerAction() {
var contentOffsetX = (self.bottomView?.contentOffset.x)! + self.frame.size.width
if contentOffsetX > self.frame.size.width * 3 {
// 當(dāng)前視圖顯示的是第三個(gè)的時(shí)候,設(shè)置bottomView的偏移量為0
self.bottomView?.contentOffset = CGPoint(x:0,y:0)
contentOffsetX = self.frame.size.width
}
self.bottomView?.setContentOffset(CGPoint(x:contentOffsetX,y:0), animated: true)
}
// 重寫(xiě)removeFromSuperview方法墨微,用于刪除定時(shí)器道媚,否則定時(shí)器一直存在,浪費(fèi)內(nèi)存
override func removeFromSuperview() {
timer?.invalidate()
timer = nil
super.removeFromSuperview()
}
// Mark:UICollectionViewDataSource
// 設(shè)置Itmes
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4;
}
// 設(shè)置cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ID", for: indexPath)
for view : UIView in cell.contentView.subviews {
view.removeFromSuperview()
}
let imageView = UIImageView.init(frame: cell.contentView.bounds)
if indexPath.row < 3 {
imageView.image = UIImage.init(named: String(indexPath.row))
} else {
imageView.image = UIImage.init(named: String(0))
}
cell.contentView.addSubview(imageView)
return cell;
}
// Mark:UICollectionViewDelegate
// 點(diǎn)擊方法
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("您點(diǎn)擊了第 \(indexPath.row == 3 ? 0 : indexPath.row) 個(gè)");
}
}
UICollectionView 和 Timer的用法和OC基本相同翘县。Swift和OC的UI部分應(yīng)該是一致的最域,因?yàn)榈讓佣际荗penGL。我直接說(shuō)一下區(qū)別:
1.Timer:如果重復(fù)锈麸,OC是等一個(gè)間隔再執(zhí)行的镀脂,Swift是立即執(zhí)行的,所以我用了GCD延時(shí)開(kāi)啟定時(shí)器掐隐。
2.Swift 沒(méi)有 CGPointZero狗热。
??無(wú)限輪播的原理就是在最后面多添加一個(gè)和第一個(gè)相同的itme。當(dāng)你滑動(dòng)到最后一個(gè)itme時(shí)虑省,把UICollectionView的contentOffset置零匿刮,繼續(xù)向右活動(dòng)。如果不添加探颈,會(huì)給用戶一種卡頓的感覺(jué)熟丸。