xcode iCarousel Swift 示例的编程实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27734106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatic implementation of iCarousel Swift example
提问by Adnan Zahid
Here is a Storyboard example of iCarousel implementation in Swift: https://github.com/nicklockwood/iCarousel/tree/master/Examples/Swift%20Example
这是一个在 Swift 中实现 iCarousel 的 Storyboard 示例:https: //github.com/nicklockwood/iCarousel/tree/master/Examples/Swift%20Example
Particularly this page: https://github.com/nicklockwood/iCarousel/blob/master/Examples/Swift%20Example/SwiftExample/ViewController.swift
特别是这个页面:https: //github.com/nicklockwood/iCarousel/blob/master/Examples/Swift%20Example/SwiftExample/ViewController.swift
Can somebody guide me how to implement it programmatically (without Storyboards)?
有人可以指导我如何以编程方式实现它(没有故事板)?
回答by Hemant Mane
Use following code to implement iCarousel programmatically using swift:
使用以下代码使用 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
}
回答by Giang
swift 3
迅捷 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
}
}