1
9個のアイテムとそれぞれに異なるグラデーションが設定されたカルーセルがあります。 アイテムをタップして、アイテムをグレー表示するか、または目盛りを付けて上に表示することで、アイテムが選択されたことを表示できます。iCarouselタップでの表示を表示
問題は、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
}
}