// // TMSmallRoomView.m // TamuVoice // // Created by tamu on 2021/9/20. // #import "TMSmallRoomView.h" @implementation TMSmallRoomView static dispatch_once_t onceToken; static TMSmallRoomView *_roomView; + (instancetype)sharedInstance{ dispatch_once(&onceToken, ^{ _roomView = [[TMSmallRoomView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-70-15, NavigationHeight + 60, 70, 92)]; _roomView.hidden = YES; UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow; NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator]; for (UIWindow *window in frontToBackWindows) { BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen; BOOL windowIsVisible = !window.hidden && window.alpha > 0; BOOL windowLevelNormal = window.windowLevel == UIWindowLevelNormal; if(windowOnMainScreen && windowIsVisible && windowLevelNormal) { currentWindow = window; break; } } [currentWindow addSubview:_roomView]; _roomView.layer.cornerRadius = 10; _roomView.layer.masksToBounds = YES; }); return _roomView; } #pragma mark - #pragma mark --- init frame -(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { /** 初始化*/ [self initContentView]; /** RAC*/ [self initRAC]; } return self ; } #pragma mark - #pragma mark --- 视图初始化 - (void)initContentView { UIImageView * iconImageView = [[UIImageView alloc] init]; iconImageView.image = [UIImage imageNamed:@"audioChatSmallphoneicon"]; [self addSubview:iconImageView]; [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self); make.width.height.mas_equalTo(40); make.top.mas_equalTo(15); }]; self.backgroundColor = [UIColor whiteColor]; [self addSubview:self.timeLabel]; [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self); make.bottom.mas_equalTo(-15); }]; } #pragma mark --- 初始化RAC - (void)initRAC { @weakify(self) UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init]; [tap addActionBlock:^(id _Nonnull sender) { @strongify(self) if (self.enterClickBlock) { self.enterClickBlock(); } [self dismiss]; }]; [self addGestureRecognizer:tap]; //添加手势 UIPanGestureRecognizer * panRcognize = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; [panRcognize setMinimumNumberOfTouches:1]; [panRcognize setEnabled:YES]; [panRcognize delaysTouchesEnded]; [panRcognize cancelsTouchesInView]; [self addGestureRecognizer:panRcognize]; } //- (void)updateUIWithData:(TMRoomModel *)model{ // // if (![model isKindOfClass:[TMRoomModel class]]) { // return; // } // // [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.homeHead] placeholderImage:kPlaceholderHeadImage()]; // self.titleLabel.text = model.homeName; // self.idLabel.text = [NSString stringWithFormat:@"ID:%ld",(long)model.homeNumber]; // //} - (void)dismiss{ dispatch_async(dispatch_get_main_queue(), ^{ self.hidden = YES; onceToken = 0; _roomView = nil; AppDelegate.sharedAppDelegate.hasSmallAudioVideoView = NO; }); } - (void)show{ dispatch_async(dispatch_get_main_queue(), ^{ self.hidden = NO; }); } - (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer { //移动状态 UIGestureRecognizerState recState = recognizer.state; switch (recState) { case UIGestureRecognizerStateBegan: break; case UIGestureRecognizerStateChanged: { CGPoint translation = [recognizer translationInView:self.window]; recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y); } break; case UIGestureRecognizerStateEnded: { CGPoint stopPoint = CGPointMake(recognizer.view.center.x, recognizer.view.center.y); // 如果按钮超出屏幕边缘 if (stopPoint.y + self.height + 40 >= SCREEN_HEIGHT) { stopPoint = CGPointMake(stopPoint.x, SCREEN_HEIGHT - self.height/2.0-TabBarHeight - 20); //这里注意iphoneX的适配。。X的SCREEN高度算法有变化。 } if (stopPoint.x - self.width/2.0 <= 0) { stopPoint = CGPointMake(self.width/2.0, stopPoint.y); } if (stopPoint.x + self.width/2.0 >= SCREEN_WIDTH) { stopPoint = CGPointMake(SCREEN_WIDTH - self.width/2.0, stopPoint.y); } if (stopPoint.y - self.height/2.0 <= 40) { stopPoint = CGPointMake(stopPoint.x, self.height/2.0 + 50); } [UIView animateWithDuration:0.3 animations:^{ recognizer.view.center = stopPoint; }]; } break; default: break; } [recognizer setTranslation:CGPointMake(0, 0) inView:self.window]; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc] init]; _timeLabel.textColor = [UIColor colorWithHex:0x4AC7FA]; _timeLabel.font = [UIFont systemFontOfSize:16]; }return _timeLabel; } @end