Files
yinmeng-ios/xplan-ios/Main/Room/View/XPMiniRoomView.m

284 lines
8.8 KiB
Mathematica
Raw Normal View History

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>
///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;
///uid nil
@property (nonatomic,strong) NSString *currentRoomUid;
2021-12-03 17:07:21 +08:00
@end
@implementation XPMiniRoomView
+ (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
- (void)configRoomMiniView:(RoomInfoModel *)roomInfo {
self.hidden = NO;
self.currentRoomUid = [NSString stringWithFormat:@"%ld", roomInfo.uid];
2021-12-03 17:07:21 +08:00
self.roomInfo = roomInfo;
[self startAvatarAnimation];
}
- (void)hiddenRoomMiniView {
self.currentRoomUid = nil;
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:)];
pan.delaysTouchesBegan = YES;
2021-12-03 17:07:21 +08:00
[self addGestureRecognizer:pan];
UITapGestureRecognizer *enterRoomTag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterRomRecognizer:)];
[self.avatarImageView addGestureRecognizer:enterRoomTag];
}
- (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"];
}
#pragma mark - Event Response
- (void)closeButtonAction:(UIButton *)sender {
[[RtcManager instance] exitRoom];
[self.avatarImageView.layer removeAllAnimations];
[self.noteView stopAnimation];
[self removeFromSuperview];
}
- (void)enterRomRecognizer:(UITapGestureRecognizer *)tap {
if (self.roomInfo.uid > 0 && self.controller) {
[self removeFromSuperview];
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
[XPRoomViewController openRoom:roomUid viewController:self.controller];
}
}
- (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