如何在iOS iCarousel上正确使用iCarouselOptions?

时间:2018-10-28 01:59:29

标签: ios swift icarousel

我正在使用Swift创建一个iOS应用,并正在使用来自GitHub的iCarousel(通过cocoapods安装),我不确定自己做错了什么,但是我似乎无法获得我设置为具有任何功能的iCarouselOptions效果...

我想要做的就是简单地更改轮播中UIView的间距(它们太靠近且重叠,这是我不希望的)。

这是我的代码:

    import UIKit
    import iCarousel

    class MiniGamesViewController: UIViewController, iCarouselDelegate, iCarouselDataSource {

        //Objects
        @IBOutlet weak var leaveMinigamesButton: UIButton!

        //Variables
        var carouselHeight : CGFloat!
        var selectionBallSize : CGFloat!

        override func viewDidLoad() {
            super.viewDidLoad()

            leaveMinigamesButton.layer.zPosition = 3
            carouselHeight = self.view.frame.size.height - 200
            selectionBallSize = self.view.frame.size.width * 0.65

            let carousel = iCarousel(frame: CGRect(
                x: 0,
                y: 100,
                width: self.view.frame.size.width,
                height: carouselHeight))

            carousel.perspective = -0.005
            carousel.centerItemWhenSelected = true
            carousel.decelerationRate = 0.9
            carousel.viewpointOffset = CGSize(width: 0, height: 0) //-selectionBallSize * 1.2

            carousel.dataSource = self
            carousel.type = .rotary
            view.addSubview(carousel)
        }

        override func viewDidAppear(_ animated: Bool) {
            setNeedsStatusBarAppearanceUpdate()
        }

        //This is how many items there will be in my rotary iCarousel
        func numberOfItems(in carousel: iCarousel) -> Int {
            return 6
        }

        //This defines each item or 'cell' in the iCarousel
        func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
            let imageView: UIImageView
            if view != nil {
                imageView = view as! UIImageView
            } else {
                imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: selectionBallSize, height: selectionBallSize))
            }
            imageView.image = UIImage(named: "orbCyan")
            return imageView
        }

        func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
            switch (option) {
            case .spacing: return value * 2
            case .radius: return 200
            case .fadeMinAlpha: return 0.5
            default: return value
            }
        }
}

具体来说,我代码的最底层是自定义的地方 应该可以生效,但是在我运行该应用程序时似乎并没有进行任何更改。此刻,我只关心尝试将轮播中的UIView隔开得更远。

我将iCarouselOptions函数放在正确的位置吗?甚至被“呼唤”吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您不知道吗?我发布此问题后,立即发现问题哈哈(经过一天的尝试)。原来我错过了简单的一行:

carousel.delegate = self

在下面:

carousel.dataSource = self

这完全可以解决问题,现在可以正常工作了。我需要新鲜的眼睛:P