如何使用iCarousel维护菜单之间的空格并为选定和未选定状态设置不同的文本颜色?

时间:2018-04-17 06:31:57

标签: ios swift icarousel

我需要将每个菜单文本空间(前导+尾随)值保持为10-15像素。并且只有选中的行文本颜色应为白色,否则其余部分应为白色withAlphaComponent值为0.4。使用iCarousel都需要完成。

最初我试图在我当前正在使用Swift 4.1版本构建的工作项目中使用类似https://github.com/hightower/HTHorizontalSelectionList的方法。

不幸的是我无法实现使用HTHorizo​​ntalSelectionList,因为在我的情况下,水平选择指示器应该居中。单击或滚动时,只有选定的菜单才会移动。这就是我与iCarousel转移的原因。除了几个要求之外,我几乎完成了90%的工作。

下面是我在iCarousel上尝试的内容,

        carouselView.delegate = self
    carouselView.dataSource = self
    carouselView.type = .linear
    carouselView.isPagingEnabled = true
    carouselView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    carouselView.clipsToBounds = true;
    carouselView.bounces = false
    carouselView.decelerationRate = 0.6; //0.6;//0.885;
    carouselView.reloadData()
    carouselView.centerItemWhenSelected = true
    carouselView .scrollToItem(at: 1, animated: false)

// MARK:iCarousel DeataSoure&代表

extension WCPPageViewController: iCarouselDataSource, iCarouselDelegate {
func numberOfItems(in carousel: iCarousel) -> Int {
    return items.count
}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {

    var label: UILabel
    label = UILabel(frame: CGRect (x: 0, y: 0, width: 165, height: 80))
    label.backgroundColor = .clear
    label.textAlignment = .center
    label.font = UIFont.WCPMontserratBold(size: 40)
    label.tag = 1
    label.textColor = UIColor.white
    label.text = "\(items[index])"
    return label
}

func carousel(_ carousel: iCarousel, didSelectItemAt index: Int) {

    redirectToOtherPageControllerClass(curentIndex: CGFloat(index))

    if currentIndex == 0 {
        updateTeamSwitchBtnLEadingAnchorValue(leadingValue: 10)
    }
    else
    {
        updateTeamSwitchBtnLEadingAnchorValue(leadingValue: -50)
    }
}

func carouselCurrentItemIndexDidChange(_ carousel: iCarousel) {

    redirectToOtherPageControllerClass(curentIndex: CGFloat(carousel.currentItemIndex))

    if carousel.currentItemIndex == 0 {
        updateTeamSwitchBtnLEadingAnchorValue(leadingValue: 10)
    }
    else
    {
        updateTeamSwitchBtnLEadingAnchorValue(leadingValue: -50)
    }
}

func carousel(_ carousel: iCarousel, itemTransformForOffset offset: CGFloat, baseTransform transform: CATransform3D) -> CATransform3D {
    //implement 'flip3D' style carousel
    let transformCopy = CATransform3DRotate(transform, CGFloat(.pi / 8.0), 0.0, 0.5, 0.0)

    return CATransform3DTranslate (transformCopy, 0, 0, offset * carousel.itemWidth)
}

func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {

    switch (option) {

    case .wrap:
        return 0

    case .fadeMin:
        return -0.4//-1.5

    case .fadeRange:
        return 1.0

    case .visibleItems:
        return 3.0

    case .fadeMax:
        return 0.4//1.0

    case .spacing:
        return 0.9

    default:
        return value
    }
}}

我试图调整此数据源的值,func carousel(_ carousel:iCarousel,valueFor选项:iCarouselOption,withDefault value:CGFloat) - > CGFloat - for" .spacing:"在每个菜单文本之间保持10 - 15像素。

我也调整了" .fadeMin:,。fadeRange:和.fadeMax:"对于选定的行文本颜色应该是白色和其余菜单的文本颜色"白色withAlphaComponent值0.4"。但产出并没有达到我的预期。

已编辑: 刚才,在阅读这篇文章的帮助下,我获得了 .fadeMin:,。fadeRange:和.fadeMax:属性 - How can we change the fade color in iCarousel?

现在它正在处理选定的行和其他我正在预期的行。

我还在冲浪,在菜单文字之间实现 .Spacing 属性。

1 个答案:

答案 0 :(得分:0)

您可以尝试这样:

   func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
    switch (option) {
        case .spacing: return 8 // 8 points spacing
        default: return value
    }
}