| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- //
- // MOChatNotificationView.m
- // MiMoLive
- //
- // Created by SuperC on 2025/6/4.
- //
- #import "MOChatNotificationView.h"
- @interface MOChatNotificationView ()
- @property (nonatomic, strong) UIImageView *avatarImageView;
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UILabel *contentLabel;
- @property (nonatomic, strong) UIView *containerView;
- @end
- @implementation MOChatNotificationView
- - (instancetype)initWithModel:(V2TIMMessage *)model{
- CGFloat statusBarHeight = 0;
- if (@available(iOS 11.0, *)) {
- UIWindow *window = [UIApplication sharedApplication].keyWindow;
- statusBarHeight = window.safeAreaInsets.top;
- } else {
- statusBarHeight = 20;
- }
-
- CGFloat notificationHeight = 80;
- self = [super initWithFrame:CGRectMake(0, -notificationHeight, [UIScreen mainScreen].bounds.size.width, notificationHeight + statusBarHeight)];
- if (self) {
- self.model = model;
- [self setupUI];
- [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)]];
- }
- return self;
- }
- - (void)setupUI{
- CGFloat statusBarHeight = 0;
- if (@available(iOS 11.0, *)) {
- UIWindow *window = [UIApplication sharedApplication].keyWindow;
- statusBarHeight = window.safeAreaInsets.top;
- } else {
- statusBarHeight = 20;
- }
-
- // 创建容器视图(带阴影和圆角)
- self.containerView = [[UIView alloc] initWithFrame:CGRectMake(15, statusBarHeight + 10, self.bounds.size.width - 30, 60)];
- self.containerView.backgroundColor = [UIColor whiteColor];
- self.containerView.layer.cornerRadius = 8.0;
- self.containerView.layer.shadowColor = [UIColor blackColor].CGColor;
- self.containerView.layer.shadowOffset = CGSizeMake(0, 2);
- self.containerView.layer.shadowOpacity = 0.1;
- self.containerView.layer.shadowRadius = 4.0;
- [self addSubview:self.containerView];
-
- // 头像
- self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
- self.avatarImageView.layer.cornerRadius = 20.0;
- self.avatarImageView.clipsToBounds = YES;
- self.avatarImageView.backgroundColor = [UIColor lightGrayColor]; // 占位色
- [self.containerView addSubview:self.avatarImageView];
-
- // 标题
- self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, self.containerView.bounds.size.width - 70, 20)];
- self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
- self.titleLabel.textColor = [UIColor blackColor];
- [self.containerView addSubview:self.titleLabel];
-
- // 内容
- self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 32, self.containerView.bounds.size.width - 70, 18)];
- self.contentLabel.font = [UIFont systemFontOfSize:14];
- self.contentLabel.textColor = [UIColor darkGrayColor];
- self.contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
- [self.containerView addSubview:self.contentLabel];
-
- // 更新数据
- [self updateWithModel:self.model];
- }
- - (void)updateWithModel:(V2TIMMessage *)model{
- NSString *contentStr = @"";
- if(model.elemType == V2TIM_ELEM_TYPE_TEXT) {
- contentStr = model.textElem.text;
- }
- else if(model.elemType == V2TIM_ELEM_TYPE_IMAGE) {
- contentStr = NSLocalString(@"[image]");
- }
- else if(model.elemType == V2TIM_ELEM_TYPE_SOUND) {
- contentStr = NSLocalString(@"[audio]");
- }
- else if(model.elemType == V2TIM_ELEM_TYPE_VIDEO) {
- contentStr = NSLocalString(@"[video]");
- }
- else if(model.elemType == V2TIM_ELEM_TYPE_FILE) {
- contentStr = NSLocalString(@"[file]");
- }
-
- self.titleLabel.text = model.nickName;
- self.contentLabel.text = contentStr;
- [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:model.faceURL] placeholderImage:[UIImage imageNamed:@"icon_mine_placeHolder"]];
- }
- - (void)setModel:(V2TIMMessage *)model {
- _model = model;
- [self updateWithModel:model];
- }
- - (void)handleTap {
- if ([self.delegate respondsToSelector:@selector(didTapNotificationWithChatId:)]) {
- [self.delegate didTapNotificationWithChatId:self.model];
- }
- }
- #pragma mark - 动画
- - (void)showWithAnimation {
- // 从屏幕顶部滑入
- [UIView animateWithDuration:0.3
- delay:0
- usingSpringWithDamping:0.7
- initialSpringVelocity:0.5
- options:UIViewAnimationOptionCurveEaseOut
- animations:^{
- self.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
- } completion:nil];
- }
- - (void)dismissWithAnimation {
- // 滑出屏幕顶部
- [UIView animateWithDuration:0.2
- delay:0
- options:UIViewAnimationOptionCurveEaseIn
- animations:^{
- self.frame = CGRectMake(0, -80, self.bounds.size.width, 80);
- } completion:^(BOOL finished) {
- if (finished) {
- [self removeFromSuperview];
- }
- }];
- }
- @end
|