JXSegmentedTitleGradientCell.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // JXSegmentedTitleGradientCell.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2019/1/23.
  6. // Copyright © 2019 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. open class JXSegmentedTitleGradientCell: JXSegmentedTitleCell {
  10. public let gradientLayer = CAGradientLayer()
  11. private var canStartSelectedAnimation: Bool = false
  12. open override func commonInit() {
  13. super.commonInit()
  14. titleLabel.removeFromSuperview()
  15. maskTitleLabel.removeFromSuperview()
  16. gradientLayer.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor, UIColor.green.cgColor]
  17. contentView.layer.addSublayer(gradientLayer)
  18. gradientLayer.mask = titleLabel.layer
  19. }
  20. open override func layoutSubviews() {
  21. super.layoutSubviews()
  22. CATransaction.begin()
  23. CATransaction.setDisableActions(true)
  24. gradientLayer.frame = titleLabel.frame
  25. CATransaction.commit()
  26. titleLabel.frame = gradientLayer.bounds
  27. }
  28. open override func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  29. super.reloadData(itemModel: itemModel, selectedType: selectedType)
  30. guard let myItemModel = itemModel as? JXSegmentedTitleGradientItemModel else {
  31. return
  32. }
  33. if myItemModel.isSelectedAnimable && canStartSelectedAnimation(itemModel: myItemModel, selectedType: selectedType) {
  34. let closure: JXSegmentedCellSelectedAnimationClosure = {[weak self] (percent) in
  35. if myItemModel.isSelected {
  36. myItemModel.titleCurrentGradientColors = JXSegmentedViewTool.interpolateColors(from: myItemModel.titleNormalGradientColors, to: myItemModel.titleSelectedGradientColors, percent: percent)
  37. }else {
  38. myItemModel.titleCurrentGradientColors = JXSegmentedViewTool.interpolateColors(from: myItemModel.titleSelectedGradientColors, to: myItemModel.titleNormalGradientColors, percent: percent)
  39. }
  40. CATransaction.begin()
  41. CATransaction.setDisableActions(true)
  42. self?.gradientLayer.colors = myItemModel.titleCurrentGradientColors
  43. CATransaction.commit()
  44. self?.setNeedsLayout()
  45. self?.layoutIfNeeded()
  46. }
  47. appendSelectedAnimationClosure(closure: closure)
  48. canStartSelectedAnimation = true
  49. startSelectedAnimationIfNeeded(itemModel: myItemModel, selectedType: selectedType)
  50. canStartSelectedAnimation = false
  51. }else {
  52. CATransaction.begin()
  53. CATransaction.setDisableActions(true)
  54. gradientLayer.startPoint = myItemModel.titleGradientStartPoint
  55. gradientLayer.endPoint = myItemModel.titleGradientEndPoint
  56. gradientLayer.colors = myItemModel.titleCurrentGradientColors
  57. CATransaction.commit()
  58. }
  59. }
  60. open override func startSelectedAnimationIfNeeded(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  61. if canStartSelectedAnimation {
  62. super.startSelectedAnimationIfNeeded(itemModel: itemModel, selectedType: selectedType)
  63. }
  64. }
  65. }