| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- //
- // ClassTitleView.m
- // StudentDaya
- //
- // Created by Kyle on 2020/6/12.
- // Copyright © 2020 DayaMusic. All rights reserved.
- //
- #import "ClassTitleView.h"
- #import "ClassroomService.h"
- #import "Masonry.h"
- #import <AVFoundation/AVFoundation.h>
- #import "RTCService.h"
- #define TSignalImageViewWidth (24)
- #define TTimeLableWidth (70)
- #define TTimeLableHeight (14)
- #define StateButtonWidth (34)
- #define TButtonWidth (24)
- #define TButtonSpace (20)
- #define THiddenDuration (3)
- @interface ClassTitleView ()
- //@property (nonatomic, strong) UILabel *timeLable;
- @property (nonatomic, strong) NSTimer *timeTimer;
- @property (nonatomic, strong) NSMutableArray *buttonArray;
- @property (nonatomic, strong) NSArray *buttonImageArray;
- @property (nonatomic, strong) NSArray *buttonHighlightedImageArray;
- @property (nonatomic, strong) NSArray *buttonCloseImageArray;
- @property (nonatomic, assign) NSInteger duration;
- @property (nonatomic, assign) NSInteger hiddenDuration;
- @property (nonatomic, assign) BOOL isCountDown;
- @end
- @implementation ClassTitleView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- self.backgroundColor = [HexRGB(0x141414) colorWithAlphaComponent:0.74];
- [self.buttonArray addObjectsFromArray:@[self.switchLineBtn,self.switchCameraBtn,self.microphoneBtn,self.cameraBtn,self.hangupBtn]];
- [self addSubviews];
- self.isCountDown = NO;
- self.duration = [ClassroomService sharedService].currentRoom.surplusTime;
- [self refreshTitleView];
- [self.timeTimer setFireDate:[NSDate distantPast]];
- }
- return self;
- }
- - (void)addSubviews {
- [self addSubview:self.signalImageView];
- // [self addSubview:self.timeLable];
- [self addSubview:self.switchLineBtn];
- [self addSubview:self.switchCameraBtn];
- [self addSubview:self.microphoneBtn];
- [self addSubview:self.cameraBtn];
- // [self addSubview:self.onShowLabel];
-
- [self addSubview:self.hangupBtn];
- CGFloat topOffset = (self.bounds.size.height - TButtonWidth) / 2.0;
-
- [self.signalImageView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.mas_left).offset(20);
- make.centerY.mas_equalTo(self.mas_centerY);
- make.width.mas_equalTo(TSignalImageViewWidth);
- make.height.mas_equalTo(TSignalImageViewWidth);
- }];
- // [self.timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.left.equalTo(self.signalImageView.mas_right).offset(20);
- // make.centerY.equalTo(self.mas_centerY);
- // make.width.equalTo(@TTimeLableWidth);
- // make.height.equalTo(@TTimeLableHeight);
- // }];
-
- // 切换线路
- [self.switchLineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.signalImageView.mas_right).offset(20);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonWidth);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
-
- // 切换摄像头
- [self.switchCameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.switchLineBtn.mas_right).offset(TButtonSpace);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonWidth);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
- // 麦克风
- [self.microphoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.switchCameraBtn.mas_right).offset(TButtonSpace);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonWidth);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
- // 摄像头
- [self.cameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.microphoneBtn.mas_right).offset(TButtonSpace);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonWidth);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
-
- // 演示label
- // [self.onShowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.left.mas_equalTo(self.cameraBtn.mas_right).offset(TButtonSpace * 2);
- // make.width.mas_equalTo(120);
- // make.height.mas_equalTo(TButtonWidth);
- // make.top.mas_equalTo(self.mas_top).offset(topOffset);
- // }];
-
- // 退出
- [self.hangupBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.mas_equalTo(self.mas_right).offset(-TButtonSpace);
- make.width.mas_equalTo(TButtonWidth);
- make.height.mas_equalTo(TButtonWidth);
- make.top.mas_equalTo(self.mas_top).offset(topOffset);
- }];
-
- self.isDisplay = YES;
- }
- - (void)clearUnreadMessage{
- Classroom *currentRoom = [ClassroomService sharedService].currentRoom;
- [[RCIMClient sharedRCIMClient] clearMessagesUnreadStatus:ConversationType_GROUP targetId:currentRoom.roomId];
- UIButton *button = (UIButton *)[self viewWithTag:ClassTitleViewActionTagChat];
- [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateNormal];
- [button setBackgroundImage:[UIImage imageNamed:@"classnews_nomal"] forState:UIControlStateHighlighted];
- }
- - (void)refreshTitleView {
- [self setDefaultButtons];
- RoomMember *curMember = [ClassroomService sharedService].currentRoom.currentMember;
- // 判断显示状态
- [self isCameraAvailable:^(bool avilable) {
- if (avilable) {
- self.cameraBtn.selected = !curMember.cameraEnable;
- }else {
- self.cameraBtn.selected = YES;
- }
- }];
- [self isMicrophoneAvailable:^(bool avilable) {
- if (avilable) {
- self.microphoneBtn.selected = !curMember.microphoneEnable;
- }else {
- self.microphoneBtn.selected = YES;
- }
- }];
- }
- - (void)setDuration:(NSInteger)duration {
- _duration = duration;
- if (_duration < 300 && self.isCountDown == NO) {
- self.isCountDown = YES;
- // self.timeLable.textColor = HexRGB(0x2DC7AA);
- // [self.timeLable.layer addAnimation:[self opacityForeverAnimation:0.5f] forKey:nil];
- }
- }
- - (void)timeFunction:(NSTimer *)timer {
- // 隐藏titleView
- if (self.isDisplay) {
- if (self.hiddenDuration > 0) {
- self.hiddenDuration -= 1;
- }
- else {
- self.hiddenDuration = 0;
- self.isDisplay = NO;
- [self hiddenView];
- }
- }
-
- // self.timeLable.text = [self formatJoinTime];
- if (self.delegate && [self.delegate respondsToSelector:@selector(refreshClassStatus)]) {
- [self.delegate refreshClassStatus];
- }
- }
- - (void)stopDurationTimer {
- [self.timeTimer setFireDate:[NSDate distantFuture]];
- if (self.timeTimer.valid) {
- [self.timeTimer invalidate];
- self.timeTimer = nil;
- }
- }
- - (void)tapEvent:(UIButton *)btn {
-
- if (btn.tag == ClassTitleViewActionTagMicrophone) {
- [self isMicrophoneAvailable:^(bool avilable) {
- if (!avilable) {
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedStringFromTable(@"microphoneAvailable", @"SealClass", nil) delegate:self cancelButtonTitle:NSLocalizedStringFromTable(@"Cancel", @"SealClass", nil) otherButtonTitles:@"设置", nil];
- [alertView show];
- #pragma clang diagnostic pop
- return;
- }else {
- if(self.delegate && [self.delegate respondsToSelector:@selector(classTitleView:didTapAtTag:)]) {
- [self.delegate classTitleView:btn didTapAtTag:btn.tag];
- }
-
- }
- }];
- }
- else if (btn.tag == ClassTitleViewActionTagCamera) {
- [self isCameraAvailable:^(bool avilable) {
- if (!avilable) {
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedStringFromTable(@"cameraAvailable", @"SealClass", nil) delegate:self cancelButtonTitle:NSLocalizedStringFromTable(@"Cancel", @"SealClass", nil) otherButtonTitles:@"设置", nil];
- [alertView show];
- #pragma clang diagnostic pop
- return;
- }else {
- if(self.delegate && [self.delegate respondsToSelector:@selector(classTitleView:didTapAtTag:)]) {
- [self.delegate classTitleView:btn didTapAtTag:btn.tag];
- }
- }
- }];
- }
- else {
- if (btn.tag == ClassTitleViewActionTagChat) {
- [self clearUnreadMessage];
- }
- if(self.delegate && [self.delegate respondsToSelector:@selector(classTitleView:didTapAtTag:)]) {
- [self.delegate classTitleView:btn didTapAtTag:btn.tag];
- }
- }
- }
- - (void)setDefaultButtons {
- self.buttonImageArray = @[@"line_switch",@"camera_switch", @"userMic_on", @"userCamera_on",@"class_hangUp"];
- self.buttonCloseImageArray = @[@"line_switch",@"camera_switch", @"userMic_off", @"userCamera_off", @"class_hangUp"];
- for(int i = 0; i < self.buttonArray.count; i++) {
- UIButton *button = [self.buttonArray objectAtIndex:i];
- button.enabled = YES;
- [button setBackgroundImage:[UIImage imageNamed:[self.buttonImageArray objectAtIndex:i]] forState:UIControlStateNormal];
- [button setBackgroundImage:[UIImage imageNamed:[self.buttonCloseImageArray objectAtIndex:i]] forState:UIControlStateSelected];
- }
- }
- - (NSString *)formatJoinTime {
- if (self.duration <= 0) {
- return @"00:00:00";
- }
- NSInteger durationInteger = self.duration--;
- NSInteger durationS = durationInteger % 60;
- NSInteger durationM = ((durationInteger - durationS) / 60) % 60;
- NSInteger durationH = (durationInteger - durationS - 60 * durationM) / 3600;
- NSMutableArray * durationArr = [NSMutableArray new];
- [durationArr addObject:[NSString stringWithFormat:@"%02ld", durationH]];
- [durationArr addObject:[NSString stringWithFormat:@"%02ld", durationM]];
- [durationArr addObject:[NSString stringWithFormat:@"%02ld", durationS]];
- return [durationArr componentsJoinedByString:@":"];
- }
- - (UIImageView *)signalImageView {
- if(!_signalImageView) {
- _signalImageView = [[UIImageView alloc] init];
- _signalImageView.image = [UIImage imageNamed:@"network_status1"];
- }
- return _signalImageView;
- }
- //- (UILabel *)timeLable {
- // if(!_timeLable) {
- // _timeLable = [[UILabel alloc] init];
- // _timeLable.font = [UIFont systemFontOfSize:14];
- // _timeLable.textColor = [UIColor colorWithHexString:@"FFFFFF" alpha:1];
- // }
- // return _timeLable;
- //}
- - (UIButton *)switchLineBtn {
- if (!_switchLineBtn) {
- _switchLineBtn = [[UIButton alloc] init];
- _switchLineBtn.enabled = YES;
- _switchLineBtn.tag = ClassTitleViewActionTagSwitchLine;
- [_switchLineBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _switchLineBtn;
- }
- - (UIButton *)switchCameraBtn {
- if(!_switchCameraBtn) {
- _switchCameraBtn = [[UIButton alloc] init];
- _switchCameraBtn.enabled = YES;
- _switchCameraBtn.tag = ClassTitleViewActionTagSwitchCamera;
- [_switchCameraBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- }
- return _switchCameraBtn;
- }
- - (UIButton *)microphoneBtn {
- if(!_microphoneBtn) {
- _microphoneBtn = [[UIButton alloc] init];
- _microphoneBtn.enabled = YES;
- _microphoneBtn.tag = ClassTitleViewActionTagMicrophone;
- [_microphoneBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- }
- return _microphoneBtn;
- }
- - (UIButton *)cameraBtn {
- if (!_cameraBtn) {
- _cameraBtn = [[UIButton alloc] init];
- _cameraBtn.enabled = YES;
- _cameraBtn.tag = ClassTitleViewActionTagCamera;
- [_cameraBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _cameraBtn;
- }
- //- (UILabel *)onShowLabel {
- // if (!_onShowLabel) {
- // _onShowLabel = [[UILabel alloc] init];
- // _onShowLabel.textColor = HexRGB(0xf9f9f9);
- // _onShowLabel.font = [UIFont systemFontOfSize:14.0f];
- // _onShowLabel.text = @"您已进入演示模式";
- // }
- // return _onShowLabel;
- //}
- - (UIButton *)hangupBtn {
- if(!_hangupBtn) {
- _hangupBtn = [[UIButton alloc] init];
- _hangupBtn.enabled = YES;
- _hangupBtn.tag = ClassTitleViewActionTagHangup;
- [_hangupBtn addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside];
-
- }
- return _hangupBtn;
- }
- - (NSMutableArray *)buttonArray {
- if (!_buttonArray) {
- _buttonArray = [[NSMutableArray alloc] init];
- }
- return _buttonArray;
- }
- - (NSTimer *)timeTimer {
- if (_timeTimer == nil) {
-
- _timeTimer = [NSTimer scheduledTimerWithTimeInterval:1
- target:self
- selector:@selector(timeFunction:)
- userInfo:nil
- repeats:YES];
- [[NSRunLoop currentRunLoop] addTimer:_timeTimer forMode:NSRunLoopCommonModes];
- }
-
- return _timeTimer;
- }
- - (void)setNetStatus:(NetWorkingStatus)netStatus {
- _netStatus = netStatus;
- NSString *displayImg = @"network_status1";
- switch (netStatus) {
- case NetWorkingStatus_Good:
- displayImg = @"network_status1";
- break;
- case NetWorkingStatus_Well:
- displayImg = @"network_status2";
- break;
- case NetWorkingStatus_Bad:
- displayImg = @"network_status3";
- break;
- case NetWorkingStatus_Poor:
- displayImg = @"network_status4";
- break;
- default:
- break;
- }
- self.signalImageView.image = [UIImage imageNamed:displayImg];
- }
- #pragma mark ----- 显示和隐藏title
- - (void)displayView {
- CGRect frame = self.frame;
- self.hiddenDuration = THiddenDuration;
- if (frame.origin.y == 0) {
- return;
- }
-
- [UIView animateWithDuration:0.3f animations:^{
- CGRect frame = self.frame;
- frame.origin.y = 0;
- self.frame = frame;
- }];
- }
- - (void)hiddenView {
- CGRect frame = self.frame;
- if (frame.origin.y < 0) {
- return;
- }
- [UIView animateWithDuration:0.3f animations:^{
- CGRect frame = self.frame;
- frame.origin.y = -frame.size.height;
- self.frame = frame;
- }];
- }
- - (void)setIsDisplay:(BOOL)isDisplay {
- _isDisplay = isDisplay;
- if (isDisplay) {
- [self displayView];
- }
- }
- #pragma mark === 永久闪烁的动画 ======
- - (CABasicAnimation *)opacityForeverAnimation:(CGFloat)time {
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
- animation.fromValue = [NSNumber numberWithFloat:1.0f];
- animation.toValue = [NSNumber numberWithFloat:0.0f];//这是透明度。
- animation.autoreverses = YES;
- animation.duration = time;
- animation.repeatCount = MAXFLOAT;
- animation.removedOnCompletion = NO;
- animation.fillMode = kCAFillModeForwards;
- animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
- return animation;
- }
- #pragma mark - UIAlertViewDelegate
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
- if (buttonIndex == 1) { // 去设置界面,开启相机访问权限
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
- }
- }
- #pragma clang diagnostic pop
- - (void)isCameraAvailable:(void (^)(bool avilable))successBlock {
- NSString *mediaType = AVMediaTypeVideo; //读取媒体类型
- AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; //读取设备授权状态
- if (AVAuthorizationStatusAuthorized == authStatus) {
- successBlock(YES);
- } else if(authStatus == AVAuthorizationStatusNotDetermined) {
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
- completionHandler:^(BOOL granted) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (granted) {
- successBlock(YES);
- } else {
- successBlock(NO);
- }
- });
- }];
- }else {
- successBlock(NO);
- }
- }
- -(void)isMicrophoneAvailable:(void (^)(bool avilable))successBlock {
- AVAudioSessionRecordPermission authStatus = [[AVAudioSession sharedInstance] recordPermission];
- if (AVAudioSessionRecordPermissionGranted == authStatus) {
- successBlock(YES);
- } else if(authStatus == AVAudioSessionRecordPermissionUndetermined){
- [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (granted) {
- successBlock(YES);
- } else {
- successBlock(NO);
-
- }
- });
- }];
- }else {
- successBlock(NO);
- }
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|