【问题标题】:Programmatic implementation of iCarousel Swift exampleiCarousel Swift 示例的编程实现
【发布时间】:2015-01-01 19:54:11
【问题描述】:

这是一个在 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

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

【问题讨论】:

    标签: ios xcode swift ios8 icarousel


    【解决方案1】:

    斯威夫特 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
            }
        }
    

    【讨论】:

      【解决方案2】:

      使用以下代码使用 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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-13
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多