程序化实现iCarousel Swift示例

时间:2015-01-01 19:54:11

标签: ios xcode swift ios8 icarousel

以下是Swift中iCarousel实现的Storyboard示例:https://github.com/nicklockwood/iCarousel/tree/master/Examples/Swift%20Example

特别是这个页面: https://github.com/nicklockwood/iCarousel/blob/master/Examples/Swift%20Example/SwiftExample/ViewController.swift

有人可以指导我如何以编程方式实现它(没有Storyboard)吗?

2 个答案:

答案 0 :(得分:0)

使用以下代码以编程方式使用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
}

答案 1 :(得分:0)

swift 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
        }
    }