轮播图的实现采用OC库的SDCycleBannerView;
说明:
项目以Pod库的形式引入;
pod 'SDCycleScrollView','~> 1.82'
必要步骤:
1.导入SDCycleScrollView 库;
import SDCycleScrollView
2.初始化轮播对象:
lazy var bannerView:SDCycleScrollView = {
let rect = CGRect(origin:CGPoint.init(x: 12, y: 15), size: CGSize.init(width: view.frame.size.width, height: 170))
let bannerView:SDCycleScrollView = SDCycleScrollView(frame: rect, shouldInfiniteLoop: true, imageNamesGroup: ["home_banner_default"])
bannerView.autoScrollTimeInterval = 3.0
bannerView.showPageControl = true
bannerView.hidesForSinglePage = false
bannerView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic
bannerView.pageDotColor = UIColor.lightGray
if #available(iOS 11.0, *) {
bannerView.currentPageDotColor = UIColor.init(named: "GreenColor")
} else {
// Fallback on earlier versions
bannerView.currentPageDotColor = UIColor.green
}
bannerView.delegate = self
return bannerView
}()
3.自定义轮播图的Cell
class CarouselsCollectionViewCell: SDCollectionViewCell {}
4.处理回调数据;
遵守协议
class DBViewController: UIViewController,SDCycleScrollViewDelegate {}
设置代理对象:
bannerView.delegate = self
func setupCustomCell(_ cell1: UICollectionViewCell!, for index: Int, cycleScrollView view: SDCycleScrollView!) {}
func customCollectionViewCellClass(for view: SDCycleScrollView!) -> AnyClass! {
return CarouselsCollectionViewCell.self
}