iCarousel显示视图

时间:2017-07-17 06:17:13

标签: ios swift icarousel

所以我设置了一个带有9个项目的旋转木马,每个项目都有不同的渐变。 我希望能够点击一个项目,并通过将其显示为灰色或在顶部应用视图来显示它已被选中。

问题是,我不知道如何将视图仅应用于已被点击的项目,因为didSelectItemAt和viewForItemAt函数是分开的。

TLDR:我正试图通过在项目顶部应用视图来选择旋转木马上的项目。

我的代码:

// Configure slides
var carouselSlides = ["1","2","3","4","5", "6", "7", "8", "9"]

// Set gradient colours
var gradientColours = [

// 0 1
    UIColor(red:0.00, green:0.76, blue:1.00, alpha:1.0),
    UIColor(red:1.00, green:1.00, blue:0.11, alpha:1.0),
// 2 3
    UIColor(red:0.38, green:0.02, blue:0.37, alpha:1.0),
    UIColor(red:0.67, green:0.03, blue:0.42, alpha:1.0),
// 4 5
    UIColor(red:0.08, green:0.12, blue:0.19, alpha:1.0),
    UIColor(red:0.14, green:0.23, blue:0.33, alpha:1.0),
// 6 7
    UIColor(red:0.09, green:0.75, blue:0.99, alpha:1.0),
    UIColor(red:0.80, green:0.19, blue:0.40, alpha:1.0),
// 8 9
    UIColor(red:0.28, green:0.33, blue:0.39, alpha:1.0),
    UIColor(red:0.16, green:0.20, blue:0.24, alpha:1.0),
// 10 11
    UIColor(red:0.00, green:0.00, blue:0.00, alpha:1.0),
    UIColor(red:0.91, green:0.30, blue:0.24, alpha:1.0),
// 12 13
    UIColor(red:0.38, green:0.42, blue:0.53, alpha:1.0),
    UIColor(red:0.25, green:0.30, blue:0.42, alpha:1.0),
// 14 15
    UIColor(red:0.11, green:0.17, blue:0.39, alpha:1.0),
    UIColor(red:0.97, green:0.80, blue:0.85, alpha:1.0),
// 16 17
    UIColor(red:1.00, green:0.32, blue:0.18, alpha:1.0),
    UIColor(red:0.87, green:0.14, blue:0.46, alpha:1.0)]


let indexOfGradientColor = 0
var slideIndex = 0

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.


    // Carousel configuration
    carouselView.type = .coverFlow
    carouselView.scrollToItem(at: carouselSlides.count / 2, animated: false)

    // Gradient background
    let gradient = CAGradientLayer()
    gradient.frame = view.bounds
    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.endPoint = CGPoint(x: 1.0, y: 1.0)

    //Set colours
    gradient.colors = [
        UIColor(red:0.03, green:0.14, blue:0.25, alpha:1.0).cgColor,
        UIColor(red:0.02, green:0.04, blue:0.15, alpha:1.0).cgColor]

    gradientBackground.layer.addSublayer(gradient)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView
{
    let viewSizeHeight = 160
    let viewSizeWidth = 275

    let frontView = UIView(frame: CGRect(x: 0, y: 0, width: viewSizeWidth, height: viewSizeHeight))
    frontView.contentMode = .center
    frontView.layer.cornerRadius = 20;
    frontView.layer.masksToBounds = true;

    let gradient = CAGradientLayer()
    gradient.frame = frontView.bounds
    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.endPoint = CGPoint(x: 1.0, y: 1.0)

    if index == 0 {
        //1
        gradient.colors = [gradientColours[12].cgColor, gradientColours[13].cgColor]
    } else if index == 1 {
        //2
        gradient.colors = [gradientColours[10].cgColor, gradientColours[11].cgColor]
    } else if index == 2 {
        //3
        gradient.colors = [gradientColours[14].cgColor, gradientColours[15].cgColor]
    } else if index == 3 {
        //4
        gradient.colors = [gradientColours[4].cgColor, gradientColours[5].cgColor]
    } else if index == 4 {
        //5
        gradient.colors = [gradientColours[0].cgColor, gradientColours[1].cgColor]
    } else if index == 5 {
        //7
        gradient.colors = [gradientColours[6].cgColor, gradientColours[7].cgColor]
    } else if index == 6 {
        //8
        gradient.colors = [gradientColours[8].cgColor, gradientColours[9].cgColor]
    } else if index == 7 {
        //9
        gradient.colors = [gradientColours[2].cgColor, gradientColours[3].cgColor]
    } else if index == 8 {
        //10
        gradient.colors = [gradientColours[16].cgColor, gradientColours[17].cgColor]
    }

    frontView.layer.addSublayer(gradient)


    let titleLabel = UILabel(frame: CGRect(x: 8, y: -55, width: viewSizeWidth, height: viewSizeHeight))
    titleLabel.font = UIFont.systemFont(ofSize: 32, weight: UIFontWeightHeavy)
    titleLabel.textColor = UIColor.white
    titleLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
    titleLabel.numberOfLines = 4
    frontView.addSubview(titleLabel)


    titleLabel.text = "\(carouselSlides[index])"

    return frontView
}

func numberOfItems(in carousel: iCarousel) -> Int {
    return carouselSlides.count
}


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

    switch (option) {

    case .spacing:
        return value * 1.05

    case .count:
        return CGFloat(carouselSlides.count)

    default:
        return value;

    }
}

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

    switch index {
    case 0:

        break

    default:
        break
    }
}

1 个答案:

答案 0 :(得分:0)

您需要将所选索引存储到一个已选中的数组中。你需要在viewAtIndex方法中检查相同的内容。

取一个数组对象来保存索引值:

var arrySelectedIndex = NSMutableArray()

替换以下方法:

  func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView
    {
        let viewSizeHeight = 160
        let viewSizeWidth = 275

        let frontView = UIView(frame: CGRect(x: 0, y: 0, width: viewSizeWidth, height: viewSizeHeight))
        frontView.contentMode = .center
        frontView.layer.cornerRadius = 20;
        frontView.layer.masksToBounds = true;

        let gradient = CAGradientLayer()
        gradient.frame = frontView.bounds
        gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradient.endPoint = CGPoint(x: 1.0, y: 1.0)

        if arrySelectedIndex.contains(index)
        {
            // Index is selected so put your custom colog
            gradient.colors = GIVE_YOUR_OWN_COLOR_TO_SEPERATE
        }
        else
        {

            // Index is not selected so go with normal
            if index == 0 {
                //1
                gradient.colors = [gradientColours[12].cgColor, gradientColours[13].cgColor]
            } else if index == 1 {
                //2
                gradient.colors = [gradientColours[10].cgColor, gradientColours[11].cgColor]
            } else if index == 2 {
                //3
                gradient.colors = [gradientColours[14].cgColor, gradientColours[15].cgColor]
            } else if index == 3 {
                //4
                gradient.colors = [gradientColours[4].cgColor, gradientColours[5].cgColor]
            } else if index == 4 {
                //5
                gradient.colors = [gradientColours[0].cgColor, gradientColours[1].cgColor]
            } else if index == 5 {
                //7
                gradient.colors = [gradientColours[6].cgColor, gradientColours[7].cgColor]
            } else if index == 6 {
                //8
                gradient.colors = [gradientColours[8].cgColor, gradientColours[9].cgColor]
            } else if index == 7 {
                //9
                gradient.colors = [gradientColours[2].cgColor, gradientColours[3].cgColor]
            } else if index == 8 {
                //10
                gradient.colors = [gradientColours[16].cgColor, gradientColours[17].cgColor]
            }
        }
        frontView.layer.addSublayer(gradient)


        let titleLabel = UILabel(frame: CGRect(x: 8, y: -55, width: viewSizeWidth, height: viewSizeHeight))
        titleLabel.font = UIFont.systemFont(ofSize: 32, weight: UIFontWeightHeavy)
        titleLabel.textColor = UIColor.white
        titleLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
        titleLabel.numberOfLines = 4
        frontView.addSubview(titleLabel)


        titleLabel.text = "\(carouselSlides[index])"

        return frontView
    }


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

          if arrySelectedIndex.contains(index)
            {
                arrySelectedIndex.remove(index)
            }
            else
            {
                arrySelectedIndex.add(index);
            }
           carousel.reloadData()
    }