YOUPAILCMultiImageContentView.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // YOUPAILCMultiImageContentView.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/9/22.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "YOUPAILCMultiImageContentView.h"
  9. #import "YOUPAILCMultiImageAttachment.h"
  10. @interface YOUPAILCMultiImageContentView()
  11. @property(nonatomic,strong)UIView* backView;
  12. @property(nonatomic,strong)UIImageView* imgView;
  13. @property(nonatomic,assign)CGFloat currentY;
  14. @end
  15. @implementation YOUPAILCMultiImageContentView
  16. - (instancetype)initSessionMessageContentView{
  17. self = [super initSessionMessageContentView];
  18. if (self) {
  19. self.opaque = YES;
  20. UIView* testView = [[UIView alloc] initWithFrame:CGRectZero];
  21. testView.backgroundColor= [UIColor whiteColor];
  22. testView.layer.cornerRadius = 6.0;
  23. testView.clipsToBounds = YES;
  24. // UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandel:)];
  25. // [testView addGestureRecognizer:tap];
  26. self.backView = testView;
  27. self.bubbleImageView.hidden = YES;
  28. [self addSubview:testView];
  29. }
  30. return self;
  31. }
  32. - (void)refresh:(NIMMessageModel *)data{
  33. [super refresh:data];
  34. NIMCustomObject *customObject = (NIMCustomObject*)data.message.messageObject;
  35. id attachment = customObject.attachment;
  36. if ([attachment isKindOfClass:[YOUPAILCMultiImageAttachment class]]) {
  37. for (UIView* subView in self.backView.subviews) {
  38. [subView removeFromSuperview];
  39. }
  40. YOUPAILCMultiImageAttachment* attachment = (YOUPAILCMultiImageAttachment*)customObject.attachment;
  41. if (attachment.imageArray.count>0) {
  42. self.currentY = (KScreenWidth-30)*9/16;
  43. UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth-30, (KScreenWidth-30)*9/16)];
  44. imgView.backgroundColor = LCGray;
  45. imgView.userInteractionEnabled = YES;
  46. UITapGestureRecognizer* imgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgTalHandel:)];
  47. [imgView addGestureRecognizer:imgTap];
  48. self.imgView = imgView;
  49. [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight View:imgView size:CGSizeMake(6, 6)];
  50. imgView.layer.masksToBounds = YES;
  51. NSDictionary* topDict = attachment.imageArray[0];
  52. [self.backView addSubview:imgView];
  53. [self.imgView sd_setImageWithURL:[NSURL URLWithString:[topDict objectForKey:@"image"]]];
  54. UIView *bottomBkgView = [[UIView alloc]initWithFrame:CGRectMake(0, (KScreenWidth-30)*9/16-60, KScreenWidth-30, 60)];
  55. CAGradientLayer *gradientLayerBot = [CAGradientLayer layer];
  56. gradientLayerBot.frame = bottomBkgView.bounds;
  57. gradientLayerBot.colors = @[(__bridge id)HexColorFromRGBA(0x00000, 0.001).CGColor,(__bridge id)HexColorFromRGBA(0x000000, 0.2).CGColor];
  58. gradientLayerBot.startPoint = CGPointMake(0, 0);
  59. gradientLayerBot.endPoint = CGPointMake(0, 1);
  60. [bottomBkgView.layer addSublayer:gradientLayerBot];
  61. [imgView addSubview:bottomBkgView];
  62. UILabel* titLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, KScreenWidth-70, 60)];
  63. titLabel.textColor = [UIColor whiteColor];
  64. titLabel.textAlignment = NSTextAlignmentLeft;
  65. titLabel.font = LCBoldFont(18);
  66. titLabel.numberOfLines=2;
  67. titLabel.text = [topDict objectForKey:@"title"];
  68. [bottomBkgView addSubview:titLabel];
  69. for (int i= 1; i<attachment.imageArray.count; i++) {
  70. NSDictionary* dict = attachment.imageArray[i];
  71. UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(0, self.currentY, KScreenWidth-30, 100)];
  72. btn.backgroundColor = HexColorFromRGB(0x2A2935);
  73. btn.adjustsImageWhenHighlighted = NO;
  74. btn.tag = i;
  75. [btn addTarget:self action:@selector(btnClick:) forControlEvents:(UIControlEventTouchUpInside)];
  76. UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth-30-20-72, 18, 72, 72)];
  77. imgView.backgroundColor = LCGray;
  78. imgView.layer.masksToBounds = YES;
  79. [imgView sd_setImageWithURL:[NSURL URLWithString:[dict objectForKey:@"image"]]];
  80. [btn addSubview:imgView];
  81. UILabel* titLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 18, KScreenWidth-30-20-72-40, 72)];
  82. titLabel.textColor = [UIColor whiteColor];
  83. titLabel.textAlignment = NSTextAlignmentLeft;
  84. titLabel.font = LCBoldFont(18);
  85. titLabel.numberOfLines=2;
  86. titLabel.text = [dict objectForKey:@"title"];
  87. [btn addSubview:titLabel];
  88. if (i!=attachment.imageArray.count-1) {
  89. UIView* lineV = [[UIView alloc]initWithFrame:CGRectMake(20, 99.5, KScreenWidth-30-20-72-40, 0.5)];
  90. lineV.backgroundColor = HexColorFromRGB(0xe6e6e6);
  91. [btn addSubview:lineV];
  92. }
  93. [self.backView addSubview:btn];
  94. self.currentY+=100;
  95. }
  96. }else{
  97. self.currentY = 0;
  98. }
  99. }
  100. }
  101. - (void)btnClick:(UIButton*)btn{
  102. NIMKitEvent *event = [[NIMKitEvent alloc] init];
  103. event.eventName = NIMKitEventNameTapContent;
  104. event.messageModel = self.model;
  105. event.messageModel.message.localExt = @{
  106. @"serial":@(btn.tag)
  107. };
  108. [self.delegate onCatchEvent:event];
  109. }
  110. - (void)imgTalHandel:(UITapGestureRecognizer*)tap{
  111. if (tap.state == UIGestureRecognizerStateEnded) {
  112. NIMKitEvent *event = [[NIMKitEvent alloc] init];
  113. event.eventName = NIMKitEventNameTapContent;
  114. event.messageModel = self.model;
  115. event.messageModel.message.localExt = @{
  116. @"serial":@(0)
  117. };
  118. [self.delegate onCatchEvent:event];
  119. }
  120. }
  121. - (void)tapHandel:(UITapGestureRecognizer*)tap{
  122. if (tap.state == UIGestureRecognizerStateEnded) {
  123. NIMKitEvent *event = [[NIMKitEvent alloc] init];
  124. event.eventName = NIMKitEventNameTapContent;
  125. event.messageModel = self.model;
  126. [self.delegate onCatchEvent:event];
  127. }
  128. }
  129. - (void)layoutSubviews{
  130. [super layoutSubviews];
  131. UIEdgeInsets contentInsets = self.model.contentViewInsets;
  132. // CGFloat tableViewWidth = self.superview.nim_width;
  133. CGSize contentsize = [self.model contentSize:KScreenWidth];
  134. CGRect imageViewFrame = CGRectMake(contentInsets.left, contentInsets.top, contentsize.width, contentsize.height);
  135. self.backView.frame = imageViewFrame;
  136. }
  137. @end