CGXVerticalMenuCollectionCell.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // CGXVerticalMenuCollectionCell..m
  3. // CGXVerticalMenuView-OC
  4. //
  5. // Created by CGX on 2018/05/01.
  6. // Copyright © 2019 CGX. All rights reserved.
  7. //
  8. #import "CGXVerticalMenuCollectionCell.h"
  9. @interface CGXVerticalMenuCollectionCell()
  10. @property (nonatomic , strong) CGXVerticalMenuCollectionItemModel *model;
  11. @property (nonatomic , strong) NSLayoutConstraint *hotImageTop;
  12. @property (nonatomic , strong) NSLayoutConstraint *hotImageLeft;
  13. @property (nonatomic , strong) NSLayoutConstraint *hotImageRight;
  14. @property (nonatomic , strong) NSLayoutConstraint *hotImageBottom;
  15. @property (nonatomic , strong) NSLayoutConstraint *hotTitleHeight;
  16. @property (nonatomic , strong) NSLayoutConstraint *hotTitleleft;
  17. @property (nonatomic , strong) NSLayoutConstraint *hotTitleRight;
  18. @property (nonatomic , strong) NSLayoutConstraint *hotTitleBottom;
  19. @end
  20. @implementation CGXVerticalMenuCollectionCell
  21. - (instancetype)initWithFrame:(CGRect)frame
  22. {
  23. self = [super initWithFrame:frame];
  24. if (self) {
  25. [self initializeViews];
  26. }
  27. return self;
  28. }
  29. - (instancetype)initWithCoder:(NSCoder *)coder
  30. {
  31. self = [super initWithCoder:coder];
  32. if (self) {
  33. [self initializeViews];
  34. }
  35. return self;
  36. }
  37. - (void)initializeViews
  38. {
  39. self.layer.masksToBounds = YES;
  40. self.clipsToBounds = YES;
  41. self.urlImageView = [[UIImageView alloc] init];
  42. self.urlImageView.contentMode = UIViewContentModeScaleAspectFit;
  43. [self.contentView addSubview:self.urlImageView];
  44. self.urlImageView.frame = self.bounds;
  45. self.urlImageView.layer.masksToBounds = YES;
  46. self.urlImageView.clipsToBounds = YES;
  47. self.urlImageView.translatesAutoresizingMaskIntoConstraints = NO;
  48. self.titleLabel =[[UILabel alloc] init];
  49. self.titleLabel.textColor = [UIColor blackColor];
  50. self.titleLabel.font = [UIFont systemFontOfSize:14];
  51. self.titleLabel.numberOfLines = 1;
  52. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  53. [self.contentView addSubview:self.titleLabel];
  54. self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  55. self.hotImageTop = [NSLayoutConstraint constraintWithItem:self.urlImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
  56. self.hotImageLeft = [NSLayoutConstraint constraintWithItem:self.urlImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
  57. self.hotImageRight = [NSLayoutConstraint constraintWithItem:self.urlImageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
  58. self.hotImageBottom = [NSLayoutConstraint constraintWithItem:self.urlImageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
  59. [self.contentView addConstraint:self.hotImageTop];
  60. [self.contentView addConstraint:self.hotImageLeft];
  61. [self.contentView addConstraint:self.hotImageRight];
  62. [self.contentView addConstraint:self.hotImageBottom];
  63. self.hotTitleHeight = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:30];
  64. [self.titleLabel addConstraint:self.hotTitleHeight];
  65. self.hotTitleleft = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
  66. self.hotTitleRight = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
  67. self.hotTitleBottom = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
  68. [self.contentView addConstraint:self.hotTitleleft];
  69. [self.contentView addConstraint:self.hotTitleRight];
  70. [self.contentView addConstraint:self.hotTitleBottom];
  71. self.titleLabel.hidden = YES;
  72. }
  73. - (void)layoutSubviews
  74. {
  75. [super layoutSubviews];
  76. [self.contentView setNeedsLayout];
  77. [self.contentView layoutIfNeeded];
  78. }
  79. - (void)reloadData:(CGXVerticalMenuCollectionItemModel *)model
  80. {
  81. self.model = model;
  82. self.contentView.backgroundColor = model.itemBgColor;
  83. self.hotImageTop.constant = 0;
  84. self.hotImageLeft.constant = 0;;
  85. self.hotImageRight.constant = 0;
  86. self.hotImageBottom.constant = 0;
  87. self.hotTitleHeight.constant = model.itemHeight;
  88. self.hotTitleleft.constant = 0;
  89. self.hotTitleRight.constant = 0;
  90. self.hotTitleBottom.constant = 0;
  91. self.titleLabel.hidden = YES;
  92. if (model.itemText && model.itemText.length>0) {
  93. self.titleLabel.hidden = NO;
  94. self.hotImageBottom.constant = -model.itemHeight-model.itemSpace;
  95. }
  96. self.urlImageView.layer.cornerRadius = model.itemCornerRadius;
  97. self.contentView.layer.borderWidth = model.itemBborderWidth;
  98. self.contentView.layer.borderColor = [model.itemBorderColor CGColor];
  99. self.contentView.layer.cornerRadius = model.itemCornerRadius;
  100. __weak typeof(self) weakSelf = self;
  101. if ([model.itemUrlStr hasPrefix:@"http:"] || [model.itemUrlStr hasPrefix:@"https:"]) {
  102. if (model.menu_ImageCallback) {
  103. model.menu_ImageCallback(weakSelf.urlImageView, [NSURL URLWithString:model.itemUrlStr]);
  104. }
  105. } else{
  106. if (model.itemUrlStr) {
  107. UIImage *image = [UIImage imageNamed:model.itemUrlStr];
  108. if (!image) {
  109. image = [UIImage imageWithContentsOfFile:model.itemUrlStr];
  110. self.urlImageView.image = image;
  111. }
  112. }
  113. if (model.menu_ImageCallback) {
  114. model.menu_ImageCallback(weakSelf.urlImageView, [NSURL URLWithString:@""]);
  115. }
  116. }
  117. self.titleLabel.text = model.itemText;
  118. self.titleLabel.textColor = model.itemColor;
  119. self.titleLabel.font = model.itemFont;
  120. self.titleLabel.numberOfLines = model.numberOfLines;
  121. }
  122. @end