CGXVerticalMenuRoundReusableView.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // CGXVerticalMenuCollectionSectionReusableView.m
  3. // CGXVerticalMenuView-OC
  4. //
  5. // Created by CGX on 2018/05/01.
  6. // Copyright © 2019 CGX. All rights reserved.
  7. //
  8. #import "CGXVerticalMenuRoundReusableView.h"
  9. @interface CGXVerticalMenuRoundReusableView()
  10. @property (nonatomic , strong) UIImageView *bgImageView;
  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. @end
  16. @implementation CGXVerticalMenuRoundReusableView
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. self.bgImageView = [[UIImageView alloc]init];
  22. self.bgImageView.contentMode = UIViewContentModeScaleAspectFill;
  23. self.bgImageView.clipsToBounds = YES;
  24. self.bgImageView.layer.masksToBounds = YES;
  25. self.bgImageView.layer.shouldRasterize = YES; // 缓存
  26. [self addSubview:self.bgImageView];
  27. self.bgImageView.translatesAutoresizingMaskIntoConstraints = NO;
  28. self.hotImageTop = [NSLayoutConstraint constraintWithItem:self.bgImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
  29. self.hotImageLeft = [NSLayoutConstraint constraintWithItem:self.bgImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
  30. self.hotImageRight = [NSLayoutConstraint constraintWithItem:self.bgImageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
  31. self.hotImageBottom = [NSLayoutConstraint constraintWithItem:self.bgImageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
  32. [self addConstraint:self.hotImageTop];
  33. [self addConstraint:self.hotImageLeft];
  34. [self addConstraint:self.hotImageRight];
  35. [self addConstraint:self.hotImageBottom];
  36. }
  37. return self;
  38. }
  39. - (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
  40. {
  41. [super applyLayoutAttributes:layoutAttributes];
  42. self.bgImageView.frame = self.bounds;
  43. self.hotImageTop.constant = 0;
  44. self.hotImageLeft.constant = 0;
  45. self.hotImageRight.constant = 0;
  46. self.hotImageBottom.constant = 0;
  47. CGXVerticalMenuRoundLayoutAttributes *attr = (CGXVerticalMenuRoundLayoutAttributes *)layoutAttributes;
  48. _myCacheAttr = attr;
  49. [self toChangeCollectionReusableViewRoundInfoWithLayoutAttributes:attr];
  50. }
  51. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
  52. [super traitCollectionDidChange:previousTraitCollection];
  53. [self toChangeCollectionReusableViewRoundInfoWithLayoutAttributes:_myCacheAttr];
  54. }
  55. - (void)toChangeCollectionReusableViewRoundInfoWithLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
  56. CGXVerticalMenuRoundLayoutAttributes *attr = (CGXVerticalMenuRoundLayoutAttributes *)layoutAttributes;
  57. [CATransaction begin];
  58. [CATransaction setDisableActions:YES];
  59. __weak typeof(self) weakSelf = self;
  60. if (attr.myConfigModel) {
  61. CGXVerticalMenuRoundModel *model = attr.myConfigModel;
  62. UIImage *bgImage = [self gx_pageImageWithColor:model.backgroundColor];
  63. self.bgImageView.image = bgImage;
  64. if ([model.hotStr hasPrefix:@"http:"] || [model.hotStr hasPrefix:@"https:"]) {
  65. if (model.menu_ImageCallback) {
  66. model.menu_ImageCallback(weakSelf.bgImageView,[NSURL URLWithString:model.hotStr]);
  67. }
  68. } else{
  69. UIImage *bgImage = [self gx_pageImageWithColor:model.backgroundColor];
  70. if (model.hotStr && model.hotStr.length>0) {
  71. bgImage = [UIImage imageNamed:model.hotStr];
  72. if (!bgImage) {
  73. bgImage = [UIImage imageWithContentsOfFile:model.hotStr];
  74. }
  75. }
  76. self.bgImageView.image = bgImage;
  77. }
  78. if (@available(iOS 13.0, *)) {
  79. self.bgImageView.layer.borderColor = [model.borderColor resolvedColorWithTraitCollection:self.traitCollection].CGColor;
  80. } else {
  81. self.bgImageView.layer.borderColor = model.borderColor.CGColor;
  82. }
  83. self.bgImageView.layer.shadowOffset = model.shadowOffset;
  84. self.bgImageView.layer.shadowOpacity = model.shadowOpacity;
  85. self.bgImageView.layer.shadowRadius = model.shadowRadius;
  86. self.bgImageView.layer.borderWidth = model.borderWidth;
  87. self.bgImageView.layer.cornerRadius = model.cornerRadius;
  88. } else{
  89. self.bgImageView.backgroundColor = self.backgroundColor;
  90. }
  91. [CATransaction commit];
  92. }
  93. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  94. if (event.type == UIEventTypeTouches) {
  95. //touchType
  96. [self decorationViewUserDidSelectEvent];
  97. }
  98. }
  99. #pragma mark - touchEvent
  100. - (void)decorationViewUserDidSelectEvent {
  101. UICollectionView *collectionView = (UICollectionView *)self.superview;
  102. if ([collectionView isKindOfClass:[UICollectionView class]]) {
  103. //is CollectionView
  104. id <CGXVerticalMenuRoundFlowLayoutDelegate> delegate = (id <CGXVerticalMenuRoundFlowLayoutDelegate>)collectionView.delegate;
  105. if ([delegate respondsToSelector:@selector(collectionView:didSelectDecorationViewAtIndexPath:)]) {
  106. [delegate collectionView:collectionView didSelectDecorationViewAtIndexPath:_myCacheAttr.indexPath];
  107. }
  108. }
  109. }
  110. /**
  111. * @brief 根据颜色生成纯色图片
  112. *
  113. * @param color 颜色
  114. *
  115. * @return 纯色图片
  116. */
  117. - (UIImage *)gx_pageImageWithColor:(UIColor *)color {
  118. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  119. UIGraphicsBeginImageContext(rect.size);
  120. CGContextRef context = UIGraphicsGetCurrentContext();
  121. CGContextSetFillColorWithColor(context, [color CGColor]);
  122. CGContextFillRect(context, rect);
  123. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  124. UIGraphicsEndImageContext();
  125. return image;
  126. }
  127. @end