JXSegmentedTitleImageCell.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // JXSegmentedTitleImageCell.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2018/12/29.
  6. // Copyright © 2018 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. open class JXSegmentedTitleImageCell: JXSegmentedTitleCell {
  10. public let imageView = UIImageView()
  11. private var currentImageInfo: String?
  12. open override func prepareForReuse() {
  13. super.prepareForReuse()
  14. currentImageInfo = nil
  15. }
  16. open override func commonInit() {
  17. super.commonInit()
  18. imageView.contentMode = .scaleAspectFit
  19. contentView.addSubview(imageView)
  20. }
  21. open override func layoutSubviews() {
  22. super.layoutSubviews()
  23. guard let myItemModel = itemModel as? JXSegmentedTitleImageItemModel else {
  24. return
  25. }
  26. let imageSize = myItemModel.imageSize
  27. switch myItemModel.titleImageType {
  28. case .topImage:
  29. let contentHeight = imageSize.height + myItemModel.titleImageSpacing + titleLabel.bounds.size.height
  30. imageView.center = CGPoint(x: contentView.bounds.size.width/2, y: (contentView.bounds.size.height - contentHeight)/2 + imageSize.height/2)
  31. titleLabel.center = CGPoint(x: contentView.bounds.size.width/2, y: imageView.frame.maxY + myItemModel.titleImageSpacing + titleLabel.bounds.size.height/2)
  32. case .leftImage:
  33. let contentWidth = imageSize.width + myItemModel.titleImageSpacing + titleLabel.bounds.size.width
  34. imageView.center = CGPoint(x: (contentView.bounds.size.width - contentWidth)/2 + imageSize.width/2, y: contentView.bounds.size.height/2)
  35. titleLabel.center = CGPoint(x: imageView.frame.maxX + myItemModel.titleImageSpacing + titleLabel.bounds.size.width/2, y: contentView.bounds.size.height/2)
  36. case .bottomImage:
  37. let contentHeight = imageSize.height + myItemModel.titleImageSpacing + titleLabel.bounds.size.height
  38. titleLabel.center = CGPoint(x: contentView.bounds.size.width/2, y: (contentView.bounds.size.height - contentHeight)/2 + titleLabel.bounds.size.height/2)
  39. imageView.center = CGPoint(x: contentView.bounds.size.width/2, y: titleLabel.frame.maxY + myItemModel.titleImageSpacing + imageSize.height/2)
  40. case .rightImage:
  41. let contentWidth = imageSize.width + myItemModel.titleImageSpacing + titleLabel.bounds.size.width
  42. titleLabel.center = CGPoint(x: (contentView.bounds.size.width - contentWidth)/2 + titleLabel.bounds.size.width/2, y: contentView.bounds.size.height/2)
  43. imageView.center = CGPoint(x: titleLabel.frame.maxX + myItemModel.titleImageSpacing + imageSize.width/2, y: contentView.bounds.size.height/2)
  44. case .onlyImage:
  45. imageView.center = CGPoint(x: contentView.bounds.size.width/2, y: contentView.bounds.size.height/2)
  46. case .onlyTitle:
  47. titleLabel.center = CGPoint(x: contentView.bounds.size.width/2, y: contentView.bounds.size.height/2)
  48. }
  49. }
  50. open override func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  51. super.reloadData(itemModel: itemModel, selectedType: selectedType )
  52. guard let myItemModel = itemModel as? JXSegmentedTitleImageItemModel else {
  53. return
  54. }
  55. titleLabel.isHidden = false
  56. imageView.isHidden = false
  57. if myItemModel.titleImageType == .onlyTitle {
  58. imageView.isHidden = true
  59. }else if myItemModel.titleImageType == .onlyImage {
  60. titleLabel.isHidden = true
  61. }
  62. imageView.bounds = CGRect(x: 0, y: 0, width: myItemModel.imageSize.width, height: myItemModel.imageSize.height)
  63. var normalImageInfo = myItemModel.normalImageInfo
  64. if myItemModel.isSelected && myItemModel.selectedImageInfo != nil {
  65. normalImageInfo = myItemModel.selectedImageInfo
  66. }
  67. //因为`func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType)`方法会回调多次,尤其是左右滚动的时候会调用无数次。如果每次都触发图片加载,会非常消耗性能。所以只会在图片发生了变化的时候,才进行图片加载。
  68. if normalImageInfo != nil && normalImageInfo != currentImageInfo {
  69. currentImageInfo = normalImageInfo
  70. if myItemModel.loadImageClosure != nil {
  71. myItemModel.loadImageClosure!(imageView, normalImageInfo!)
  72. }else {
  73. imageView.image = UIImage(named: normalImageInfo!)
  74. }
  75. }
  76. if myItemModel.isImageZoomEnabled {
  77. imageView.transform = CGAffineTransform(scaleX: myItemModel.imageCurrentZoomScale, y: myItemModel.imageCurrentZoomScale)
  78. }else {
  79. imageView.transform = .identity
  80. }
  81. setNeedsLayout()
  82. }
  83. }