HjzCarouselView
适用于 Swift 的轮播图库。
支持代码、xib、Storyboard。
无限循环,可滑动。自动释放内存。
安装
CocoaPods
pod 'HjzCarouselView'
Carthage
github "HjzCy/HjzCarouselView"
使用
传入字符串数组,会自动识别是网络图片还是本地图片,可以混搭网络图片和本地图片,优先加载本地图片。
如果无法加载 http 图片,需要在 info.plist 中添加以下内容:App Transport Security Settings -> Allow Arbitrary Loads (true)
/// ## 图片模式
let headerView = CarouselView(frame: CGRect(x: 0, y: 44, width: view.frame.width, height: 230), placeholder: "nopic")
/// 开始滑动
headerView.scroll(contents: ["http://pic34.photophoto.cn/20150202/0005018384491898_b.jpg",
"http://pic35.photophoto.cn/20150424/0006019070887493_b.jpg",
"ydms1",
"ydms2",
"ydms3"], style: .image)
/// 点击回调
headerView.tapClosure = { (index) in
print("HeaderView current index: \(index)")
}
view.addSubview(headerView)
/// ## 头条模式
let headline = CarouselView(frame: CGRect(x: 0, y: headerView.frame.maxY + 40, width: view.frame.width, height: 44))
/// 设置左边间隔
headline.headlineSpace.left = 5
/// 开始滑动
headline.scroll(contents: ["“迎风一刀斩”,常山药业跌停!网红公司高管神操作,让韭菜情何以堪",
"Google Chrome 将从9月开始,默认 HTTPS ...",
"北约军人在乌克兰顿巴斯地区遇爆炸 至少3死2伤",
"[图]Chrome将逐步移除绿色小锁和“Secure”标记"], style: .label)
/// 点击回调
headline.tapClosure = { (index) in
print("Headline current index: \(index)")
}
view.addSubview(headline)
APIs
/// 头条字号
public var fontSize: CGFloat = 14
/// 头条左侧间隔,可在 xib 中直接设置
@objc dynamic public var headlineLeft: CGFloat = 0
/// 头条右侧间隔,可在 xib 中直接设置
@objc dynamic public var headlineRight: CGFloat = 0
/// 占位图,如果是在 xib 上,可以直接通过 keyPath 设置
@objc dynamic public var placeholder: UIImage?
/// 索引控件风格色
public var pageIndicatorTintColor = UIColor.lightText
/// 当前所在的索引颜色
public var currentPageIndicatorTintColor = UIColor.orange
更新记录:
0.0.18:
修复了初始化时图片的默认样式不应该是 .scaleAspectFill
的问题。
修复了调用 scroll()
方法后,有时候不会自动滑动的问题。
0.0.19:
修复了在 .label
样式下视图没有显示的问题。
优化了代码。