Swift实现轮播图

CCAutoScrollView主要实现思想是让用户可自定义需要展示的轮播图,更好的满足用户的需求

GitHub - cheyongzi/CCAutoScrollView: 更加开放的AutoScrollView,可自定义需要显示的view

Default AutoScrollView

let scroll = CCAutoScrollView(frame: CGRect(x: 100, y: 100, width: 200, height: 100))
scroll.dataSource = ["1.jpg","2.jpg","3.jpg"]
scroll.autoScrollEnable = true //or scroll.autoScrollTimeInterval = 1.5
scroll.delegate = self
view.addSubview(scroll)

Custom AutoScrollView

let scroll = CCAutoScrollView(frame: CGRect(x: 0, y: 64, width: 300, height: 200))
scroll.dataSource = [UIColor.red,UIColor.blue,UIColor.green]
scroll.autoScrollTimeInterval = 1.5
scroll.cellNibName = "CustomCollectionViewCell"
scroll.cellConfig = { (cell, data) in
guard let customCell = cell as? CustomCollectionViewCell else {
return
}
guard let color = data as? UIColor else {
return
}
customCell.customView.backgroundColor = color
}
view.addSubview(scroll)

XIB AutoScrollView

@IBOutlet weak var autoScrollView: CCAutoScrollView!
override func viewDidLoad() {
super.viewDidLoad()
autoScrollView.dataSource = ["http://","http://","http://"]
autoScrollView.cellClass = CustomClassCollectionViewCell.self
autoScrollView.autoScrollTimeInterval = 2.5
autoScrollView.cellConfig = { (cell, data) in
guard let customCell = cell as? CustomClassCollectionViewCell else {
return
}
guard let imgUrlString = data as? String else {
return
}
DispatchQueue.global().async {
let imageData = Data(contentsOf: URL(string: imgUrlString))
DispatchQueue.main.async {
customCell.imageView.image = UIImage(data: imageData)
}
}
//you can use Kingfisher download and cached image
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容