引入
轮播广告控件 纯SWIFT
pod 'FSPagerView'
lazy var pagerView = FSPagerView()
lazy var pageControl = FSPageControl()
self.pagerView.dataSource = self
self.pagerView.delegate = self
self.pagerView.automaticSlidingInterval = 3
self.pagerView.isInfinite = !pagerView.isInfinite
//self.pagerView.interitemSpacing = 50
self.pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
self.addSubview(self.pagerView)
self.pagerView.snp.makeConstraints { (make) in
make.width.equalTo(self)
make.height.equalTo(210+YYStatusBarHeight)
make.left.equalTo(0)
}
self.pageControl.numberOfPages = imageNames.count
self.pageControl.contentHorizontalAlignment = .center
pageControl.setFillColor(.gray, for: .normal)
pageControl.setFillColor(AppBlue, for: .selected)
self.addSubview(self.pageControl)
self.pageControl.snp.makeConstraints { (make) in
make.width.equalTo(80)
make.height.equalTo(30)
make.bottom.equalTo(pagerView.snp.bottom).offset(-20)
make.centerX.equalTo(self)
}
//实现代理 :FSPagerViewDataSource, FSPagerViewDelegate
// MARK:- FSPagerView Delegate
func numberOfItems(in pagerView: FSPagerView) -> Int {
return imageNames.count
}
func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
cell.imageView?.downloadedFrom(imageurl: self.imageNames[index].picture_addr!)
// cell.imageView?.load.request(with: )
return cell
}
func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
pagerView.deselectItem(at: index, animated: true)
pagerView.scrollToItem(at: index, animated: true)
self.pageControl.currentPage = index
}
func pagerViewDidScroll(_ pagerView: FSPagerView) {
guard self.pageControl.currentPage != pagerView.currentIndex else {
return
}
self.pageControl.currentPage = pagerView.currentIndex // Or Use KVO with property "currentIndex"
}
//加载数据
var imageNames = [BannerHomeModel]() {
didSet {
self.pageControl.numberOfPages = imageNames.count
self.pagerView.reloadData()
}
}