MOChatNotificationView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // MOChatNotificationView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2025/6/4.
  6. //
  7. #import "MOChatNotificationView.h"
  8. @interface MOChatNotificationView ()
  9. @property (nonatomic, strong) UIImageView *avatarImageView;
  10. @property (nonatomic, strong) UILabel *titleLabel;
  11. @property (nonatomic, strong) UILabel *contentLabel;
  12. @property (nonatomic, strong) UIView *containerView;
  13. @end
  14. @implementation MOChatNotificationView
  15. - (instancetype)initWithModel:(V2TIMMessage *)model{
  16. CGFloat statusBarHeight = 0;
  17. if (@available(iOS 11.0, *)) {
  18. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  19. statusBarHeight = window.safeAreaInsets.top;
  20. } else {
  21. statusBarHeight = 20;
  22. }
  23. CGFloat notificationHeight = 80;
  24. self = [super initWithFrame:CGRectMake(0, -notificationHeight, [UIScreen mainScreen].bounds.size.width, notificationHeight + statusBarHeight)];
  25. if (self) {
  26. self.model = model;
  27. [self setupUI];
  28. [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)]];
  29. }
  30. return self;
  31. }
  32. - (void)setupUI{
  33. CGFloat statusBarHeight = 0;
  34. if (@available(iOS 11.0, *)) {
  35. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  36. statusBarHeight = window.safeAreaInsets.top;
  37. } else {
  38. statusBarHeight = 20;
  39. }
  40. // 创建容器视图(带阴影和圆角)
  41. self.containerView = [[UIView alloc] initWithFrame:CGRectMake(15, statusBarHeight + 10, self.bounds.size.width - 30, 60)];
  42. self.containerView.backgroundColor = [UIColor whiteColor];
  43. self.containerView.layer.cornerRadius = 8.0;
  44. self.containerView.layer.shadowColor = [UIColor blackColor].CGColor;
  45. self.containerView.layer.shadowOffset = CGSizeMake(0, 2);
  46. self.containerView.layer.shadowOpacity = 0.1;
  47. self.containerView.layer.shadowRadius = 4.0;
  48. [self addSubview:self.containerView];
  49. // 头像
  50. self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
  51. self.avatarImageView.layer.cornerRadius = 20.0;
  52. self.avatarImageView.clipsToBounds = YES;
  53. self.avatarImageView.backgroundColor = [UIColor lightGrayColor]; // 占位色
  54. [self.containerView addSubview:self.avatarImageView];
  55. // 标题
  56. self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, self.containerView.bounds.size.width - 70, 20)];
  57. self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
  58. self.titleLabel.textColor = [UIColor blackColor];
  59. [self.containerView addSubview:self.titleLabel];
  60. // 内容
  61. self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 32, self.containerView.bounds.size.width - 70, 18)];
  62. self.contentLabel.font = [UIFont systemFontOfSize:14];
  63. self.contentLabel.textColor = [UIColor darkGrayColor];
  64. self.contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  65. [self.containerView addSubview:self.contentLabel];
  66. // 更新数据
  67. [self updateWithModel:self.model];
  68. }
  69. - (void)updateWithModel:(V2TIMMessage *)model{
  70. NSString *contentStr = @"";
  71. if(model.elemType == V2TIM_ELEM_TYPE_TEXT) {
  72. contentStr = model.textElem.text;
  73. }
  74. else if(model.elemType == V2TIM_ELEM_TYPE_IMAGE) {
  75. contentStr = NSLocalString(@"[image]");
  76. }
  77. else if(model.elemType == V2TIM_ELEM_TYPE_SOUND) {
  78. contentStr = NSLocalString(@"[audio]");
  79. }
  80. else if(model.elemType == V2TIM_ELEM_TYPE_VIDEO) {
  81. contentStr = NSLocalString(@"[video]");
  82. }
  83. else if(model.elemType == V2TIM_ELEM_TYPE_FILE) {
  84. contentStr = NSLocalString(@"[file]");
  85. }
  86. self.titleLabel.text = model.nickName;
  87. self.contentLabel.text = contentStr;
  88. [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:model.faceURL] placeholderImage:[UIImage imageNamed:@"icon_mine_placeHolder"]];
  89. }
  90. - (void)setModel:(V2TIMMessage *)model {
  91. _model = model;
  92. [self updateWithModel:model];
  93. }
  94. - (void)handleTap {
  95. if ([self.delegate respondsToSelector:@selector(didTapNotificationWithChatId:)]) {
  96. [self.delegate didTapNotificationWithChatId:self.model];
  97. }
  98. }
  99. #pragma mark - 动画
  100. - (void)showWithAnimation {
  101. // 从屏幕顶部滑入
  102. [UIView animateWithDuration:0.3
  103. delay:0
  104. usingSpringWithDamping:0.7
  105. initialSpringVelocity:0.5
  106. options:UIViewAnimationOptionCurveEaseOut
  107. animations:^{
  108. self.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
  109. } completion:nil];
  110. }
  111. - (void)dismissWithAnimation {
  112. // 滑出屏幕顶部
  113. [UIView animateWithDuration:0.2
  114. delay:0
  115. options:UIViewAnimationOptionCurveEaseIn
  116. animations:^{
  117. self.frame = CGRectMake(0, -80, self.bounds.size.width, 80);
  118. } completion:^(BOOL finished) {
  119. if (finished) {
  120. [self removeFromSuperview];
  121. }
  122. }];
  123. }
  124. @end