【发布时间】:2015-01-01 19:54:11
【问题描述】:
这是一个在 Swift 中实现 iCarousel 的 Storyboard 示例:https://github.com/nicklockwood/iCarousel/tree/master/Examples/Swift%20Example
有人可以指导我如何以编程方式实现它(没有情节提要)吗?
【问题讨论】:
标签: ios xcode swift ios8 icarousel
这是一个在 Swift 中实现 iCarousel 的 Storyboard 示例:https://github.com/nicklockwood/iCarousel/tree/master/Examples/Swift%20Example
有人可以指导我如何以编程方式实现它(没有情节提要)吗?
【问题讨论】:
标签: ios xcode swift ios8 icarousel
斯威夫特 3
private let HEIGHT_CAROUSEL: CGFloat = 100
private func initiCarousel() {
let carousel = iCarousel(frame: CGRect(x: 0, y: view.frame.height - HEIGHT_CAROUSEL, width: view.frame.width, height: HEIGHT_CAROUSEL))
carousel.delegate = self
carousel.dataSource = self
view.addSubview(carousel)
}
// MARK: - iCarouselDelegate
extension YourNameViewController: iCarouselDataSource, iCarouselDelegate {
func numberOfItems(in carousel: iCarousel) -> Int {
return arrr.count ?? 0
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var itemView: UIImageView
itemView = UIImageView(frame: CGRect(x: 0, y: 0, width: HEIGHT_CAROUSEL, height: HEIGHT_CAROUSEL))
itemView.contentMode = .scaleAspectFill
if let image = url {
itemView.setImageWithIndicator(imageUrl: image)
}
return itemView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if (option == .spacing) {
return value * 1.1
}
return value
}
}
【讨论】:
使用以下代码使用 swift 以编程方式实现 iCarousel:
override func viewDidLoad()
{
super.viewDidLoad()
carousel = iCarousel(frame: CGRectMake(0, 0, 200, 200))
carousel.center = view.center
carousel.dataSource = self
carousel.delegate = self
}
【讨论】: