2021-12-03 17:07:21 +08:00
|
|
|
//
|
|
|
|
// XPMiniRoomView.m
|
|
|
|
// xplan-ios
|
|
|
|
//
|
|
|
|
// Created by 冯硕 on 2021/12/3.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "XPMiniRoomView.h"
|
|
|
|
///Third
|
|
|
|
#import <Masonry/Masonry.h>
|
2021-12-03 19:10:35 +08:00
|
|
|
#import <NIMSDK/NIMSDK.h>
|
2021-12-03 17:07:21 +08:00
|
|
|
///Tool
|
|
|
|
#import "XPMacro.h"
|
|
|
|
#import "ThemeColor.h"
|
|
|
|
#import "NetImageView.h"
|
|
|
|
#import "RtcManager.h"
|
|
|
|
#import "UIButton+EnlargeTouchArea.h"
|
|
|
|
#import "XPNoteView.h"
|
|
|
|
///Model
|
|
|
|
#import "RoomInfoModel.h"
|
|
|
|
///View
|
|
|
|
#import "XPRoomViewController.h"
|
|
|
|
|
|
|
|
@interface XPMiniRoomView ()
|
|
|
|
///底部的底图
|
|
|
|
@property (nonatomic,strong) UIImageView *backImageView;
|
|
|
|
///头像
|
|
|
|
@property (nonatomic,strong) NetImageView *avatarImageView;
|
|
|
|
///昵称
|
|
|
|
@property (nonatomic,strong) UILabel *nickLabel;
|
|
|
|
///id
|
|
|
|
@property (nonatomic,strong) UILabel *idLabel;
|
|
|
|
///关闭
|
|
|
|
@property (nonatomic,strong) UIButton *closeButton;
|
|
|
|
///音符
|
|
|
|
@property (nonatomic,strong) XPNoteView *noteView;
|
|
|
|
///房间最小化
|
|
|
|
@property (nonatomic,strong) RoomInfoModel *roomInfo;
|
2021-12-03 18:12:24 +08:00
|
|
|
///当前最小化的房间的uid 如果没有的话 为nil
|
|
|
|
@property (nonatomic,strong) NSString *currentRoomUid;
|
2021-12-03 19:10:35 +08:00
|
|
|
///房间的id
|
|
|
|
@property (nonatomic,strong) NSString *currentRoomId;
|
2021-12-03 17:07:21 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation XPMiniRoomView
|
|
|
|
|
2021-12-03 18:12:24 +08:00
|
|
|
+ (instancetype)shareMiniRoomView {
|
|
|
|
static dispatch_once_t onceToken = 0;
|
|
|
|
static id instance;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
instance = [[self alloc] initWithFrame:CGRectMake(KScreenWidth - 140 * 1.2, KScreenHeight - kSafeAreaBottomHeight - 150, 140 * 1.2, 34 * 1.2)];
|
|
|
|
});
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:07:21 +08:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
if (self) {
|
|
|
|
[self initEvents];
|
|
|
|
[self initSubViews];
|
|
|
|
[self initSubViewConstraints];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
#pragma mark - Public Method
|
2021-12-03 18:12:24 +08:00
|
|
|
- (void)configRoomMiniView:(RoomInfoModel *)roomInfo {
|
|
|
|
self.hidden = NO;
|
|
|
|
self.currentRoomUid = [NSString stringWithFormat:@"%ld", roomInfo.uid];
|
2021-12-03 19:10:35 +08:00
|
|
|
self.currentRoomId = [NSString stringWithFormat:@"%ld", roomInfo.roomId];
|
2021-12-03 17:07:21 +08:00
|
|
|
self.roomInfo = roomInfo;
|
|
|
|
[self startAvatarAnimation];
|
2021-12-03 23:57:34 +08:00
|
|
|
[self.noteView startAnimation];
|
2021-12-03 17:07:21 +08:00
|
|
|
}
|
|
|
|
|
2021-12-03 18:12:24 +08:00
|
|
|
- (void)hiddenRoomMiniView {
|
2021-12-03 23:57:34 +08:00
|
|
|
[self.avatarImageView.layer removeAllAnimations];
|
|
|
|
[self.noteView stopAnimation];
|
2021-12-03 18:12:24 +08:00
|
|
|
self.currentRoomUid = nil;
|
2021-12-03 19:10:35 +08:00
|
|
|
self.currentRoomId = nil;
|
2021-12-03 18:12:24 +08:00
|
|
|
self.hidden = YES;
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:07:21 +08:00
|
|
|
#pragma mark - Private Method
|
|
|
|
- (void)initSubViews {
|
|
|
|
[self addSubview:self.backImageView];
|
|
|
|
|
|
|
|
[self.backImageView addSubview:self.avatarImageView];
|
|
|
|
[self.backImageView addSubview:self.noteView];
|
|
|
|
[self.backImageView addSubview:self.nickLabel];
|
|
|
|
[self.backImageView addSubview:self.idLabel];
|
|
|
|
[self.backImageView addSubview:self.closeButton];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)initSubViewConstraints {
|
|
|
|
|
|
|
|
[self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
make.edges.mas_equalTo(self);
|
|
|
|
}];
|
|
|
|
|
|
|
|
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
make.left.mas_equalTo(self.backImageView).offset(5);
|
|
|
|
make.top.bottom.mas_equalTo(self.backImageView).inset(5);
|
|
|
|
make.height.mas_equalTo(self.avatarImageView.mas_width);
|
|
|
|
}];
|
|
|
|
|
|
|
|
[self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
make.left.mas_equalTo(self.avatarImageView.mas_right).offset(5);
|
|
|
|
make.bottom.mas_equalTo(self.avatarImageView.mas_centerY).offset(-1.5);
|
|
|
|
}];
|
|
|
|
|
|
|
|
[self.idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
make.left.mas_equalTo(self.nickLabel);
|
|
|
|
make.top.mas_equalTo(self.avatarImageView.mas_centerY).offset(1.5);
|
|
|
|
}];
|
|
|
|
|
|
|
|
[self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
make.size.mas_equalTo(CGSizeMake(15, 15));
|
|
|
|
make.centerY.mas_equalTo(self.backImageView);
|
|
|
|
make.right.mas_equalTo(self.backImageView).offset(-11);
|
|
|
|
}];
|
|
|
|
|
|
|
|
[self.noteView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
make.center.mas_equalTo(self.avatarImageView);
|
|
|
|
make.height.mas_equalTo(12);
|
|
|
|
make.width.mas_equalTo(12);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)initEvents {
|
|
|
|
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(roomMiniTag:)];
|
2021-12-03 18:12:24 +08:00
|
|
|
pan.delaysTouchesBegan = YES;
|
2021-12-03 17:07:21 +08:00
|
|
|
[self addGestureRecognizer:pan];
|
|
|
|
|
|
|
|
UITapGestureRecognizer *enterRoomTag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterRomRecognizer:)];
|
2021-12-03 23:57:34 +08:00
|
|
|
[self addGestureRecognizer:enterRoomTag];
|
2021-12-03 17:07:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startAvatarAnimation {
|
|
|
|
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
|
|
|
|
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
|
|
|
|
rotationAnimation.duration = 3;
|
|
|
|
rotationAnimation.cumulative = YES;
|
|
|
|
rotationAnimation.repeatCount = MAXFLOAT;
|
|
|
|
rotationAnimation.removedOnCompletion = NO;
|
|
|
|
[self.avatarImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
|
|
|
|
}
|
|
|
|
|
2021-12-03 23:57:34 +08:00
|
|
|
|
2021-12-03 17:07:21 +08:00
|
|
|
#pragma mark - Event Response
|
|
|
|
- (void)closeButtonAction:(UIButton *)sender {
|
2021-12-03 19:10:35 +08:00
|
|
|
[[NIMSDK sharedSDK].chatroomManager exitChatroom:self.currentRoomId completion:nil];
|
2021-12-03 17:07:21 +08:00
|
|
|
[[RtcManager instance] exitRoom];
|
2021-12-03 23:57:34 +08:00
|
|
|
[self hiddenRoomMiniView];
|
2021-12-03 17:07:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)enterRomRecognizer:(UITapGestureRecognizer *)tap {
|
2021-12-03 19:10:35 +08:00
|
|
|
if (self.currentRoomUid > 0 && self.controller) {
|
|
|
|
[XPRoomViewController openRoom:self.currentRoomUid viewController:self.controller];
|
2021-12-03 23:57:34 +08:00
|
|
|
[self hiddenRoomMiniView];
|
2021-12-03 17:07:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)roomMiniTag:(UIPanGestureRecognizer*)p {
|
|
|
|
UIWindow *appWindow = [UIApplication sharedApplication].delegate.window;
|
|
|
|
CGPoint panPoint = [p locationInView:appWindow];
|
|
|
|
if (p.state == UIGestureRecognizerStateBegan) {
|
|
|
|
self.alpha = 1;
|
|
|
|
} else if(p.state == UIGestureRecognizerStateChanged) {
|
|
|
|
self.center = CGPointMake(panPoint.x, panPoint.y);
|
|
|
|
} else if(p.state == UIGestureRecognizerStateEnded
|
|
|
|
|| p.state == UIGestureRecognizerStateCancelled) {
|
|
|
|
self.alpha = 1;
|
|
|
|
CGFloat touchWidth = self.frame.size.width;
|
|
|
|
CGFloat touchHeight = self.frame.size.height;
|
|
|
|
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
|
|
|
|
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
|
|
|
|
// fabs 是取绝对值的意思
|
|
|
|
CGFloat left = fabs(panPoint.x);
|
|
|
|
CGFloat right = fabs(screenWidth - left);
|
|
|
|
CGFloat top = fabs(panPoint.y);
|
|
|
|
CGFloat bottom = fabs(screenHeight - top);
|
|
|
|
CGFloat minSpace = 0;
|
|
|
|
minSpace = MIN(MIN(MIN(top, left), bottom), right);
|
|
|
|
CGPoint newCenter;
|
|
|
|
CGFloat targetY = 0;
|
|
|
|
//校正Y
|
|
|
|
if (panPoint.y < 15 + touchHeight / 2.0) {
|
|
|
|
targetY = 15 + touchHeight / 2.0;
|
|
|
|
}else if (panPoint.y > (screenHeight - touchHeight / 2.0 - 15)) {
|
|
|
|
targetY = screenHeight - touchHeight / 2.0 - 15;
|
|
|
|
}else{
|
|
|
|
targetY = panPoint.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minSpace == left) {
|
|
|
|
newCenter = CGPointMake(15+touchWidth/2.0, targetY);
|
|
|
|
}else if (minSpace == right) {
|
|
|
|
newCenter = CGPointMake(screenWidth - touchWidth/2.0 - 15, targetY);
|
|
|
|
}else if (minSpace == top) {
|
|
|
|
newCenter = CGPointMake(panPoint.x, touchWidth / 3);
|
|
|
|
}else {
|
|
|
|
newCenter = CGPointMake(panPoint.x, screenHeight - touchWidth / 3);
|
|
|
|
}
|
|
|
|
[UIView animateWithDuration:0.25 animations:^{
|
|
|
|
if (newCenter.y + self.frame.size.height / 2 > KScreenHeight - kSafeAreaBottomHeight - 44) {
|
|
|
|
self.center = CGPointMake(newCenter.x, KScreenHeight - self.frame.size.height / 2 - kSafeAreaBottomHeight - 44);
|
|
|
|
}else {
|
|
|
|
self.center = newCenter;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
NSLog(@"pan state : %zd", p.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Getters And Setters
|
|
|
|
- (void)setRoomInfo:(RoomInfoModel *)roomInfo {
|
|
|
|
_roomInfo = roomInfo;
|
|
|
|
if (_roomInfo) {
|
|
|
|
self.avatarImageView.imageUrl = _roomInfo.avatar;
|
|
|
|
if (_roomInfo.title.length > 8) {
|
|
|
|
_roomInfo.title = [NSString stringWithFormat:@"%@…", [_roomInfo.title substringToIndex:8]];
|
|
|
|
}
|
|
|
|
self.nickLabel.text = _roomInfo.title;
|
|
|
|
self.idLabel.text = [NSString stringWithFormat:@"%@号:%ld", AppName,_roomInfo.erbanNo];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIImageView *)backImageView {
|
|
|
|
if (!_backImageView) {
|
|
|
|
_backImageView = [[UIImageView alloc] init];
|
|
|
|
_backImageView.userInteractionEnabled = YES;
|
|
|
|
_backImageView.image = [UIImage imageNamed:@"room_mini_background"];
|
|
|
|
}
|
|
|
|
return _backImageView;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NetImageView *)avatarImageView {
|
|
|
|
if (!_avatarImageView) {
|
|
|
|
NetImageConfig * config = [[NetImageConfig alloc] init];
|
|
|
|
config.imageType = ImageTypeUserIcon;
|
|
|
|
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
|
|
|
|
_avatarImageView = [[NetImageView alloc] initWithConfig:config];
|
|
|
|
_avatarImageView.layer.masksToBounds = YES;
|
|
|
|
_avatarImageView.layer.cornerRadius = (34 * 1.2 - 10)/2;
|
|
|
|
_avatarImageView.userInteractionEnabled = YES;
|
|
|
|
}
|
|
|
|
return _avatarImageView;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UILabel *)nickLabel {
|
|
|
|
if (!_nickLabel) {
|
|
|
|
_nickLabel = [[UILabel alloc] init];
|
|
|
|
_nickLabel.font = [UIFont systemFontOfSize:12];
|
|
|
|
_nickLabel.textColor = [ThemeColor mainTextColor];
|
|
|
|
}
|
|
|
|
return _nickLabel;
|
|
|
|
}
|
|
|
|
- (UILabel *)idLabel {
|
|
|
|
if (!_idLabel) {
|
|
|
|
_idLabel = [[UILabel alloc] init];
|
|
|
|
_idLabel.font = [UIFont systemFontOfSize:10];
|
|
|
|
_idLabel.textColor = [ThemeColor mainTextColor];
|
|
|
|
}
|
|
|
|
return _idLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIButton *)closeButton {
|
|
|
|
if (!_closeButton) {
|
|
|
|
_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
|
[_closeButton setImage:[UIImage imageNamed:@"room_mini_close"] forState:UIControlStateNormal];
|
|
|
|
[_closeButton addTarget:self action:@selector(closeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[_closeButton setEnlargeEdgeWithTop:5 right:5 bottom:5 left:5];
|
|
|
|
}
|
|
|
|
return _closeButton;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (XPNoteView *)noteView {
|
|
|
|
if (!_noteView) {
|
|
|
|
_noteView = [[XPNoteView alloc] init];
|
|
|
|
_noteView.pillarColor = [UIColor colorWithWhite:1 alpha:0.6];
|
|
|
|
_noteView.pillarWidth = 2;
|
|
|
|
[_noteView startAnimation];
|
|
|
|
}
|
|
|
|
return _noteView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|