// // GiGaMaskTaskViewController.m // GIGA // // Created by lianxiang on 2018/8/14. // Copyright © 2018年 com.giga.ios. All rights reserved. // #import "GiGaMaskTaskViewController.h" #import "MaskViewBootomWaringView.h" #import "GiGaUserLoginVC.h" #import "UserGuardViewController.h" #import "GiGaUserDefault.h" #import "GiGaAppGaurdVC.h" #import "GiGaMeViewController.h" #import "GiGaAdDetailViewController.h" #import "GiGaMasssagesVC.h" #import "GiGaQuestionVC.h" #import #import "GiGaLocalNotificationManager.h" #import "AppDelegate.h" #import "LXCountTimer.h" #import "GiGaHelper.h" #import #import "GiGaMeViewController.h" #import "GiGaFlyingCommitInputView.h" #import "GiGaFlingCommitVC.h" #import "MaskTimeCircularProgressView.h" #import "UINavigationBar+Custom.h" @interface GiGaMaskTaskViewController () { AVAudioPlayer *_musicPalyer; } @property(nonatomic,strong)MaskViewBootomWaringView *waringView; @property(nonatomic,strong)UILabel *descLabl; @property(nonatomic,strong)UIView *descBackView; //@property (nonatomic,strong) GiGaFlyingCommitInputView *commitInputView; @property(nonatomic,strong) UIImageView *maskImageView; @property(nonatomic,strong) UIImageView *faceView; //@property(nonatomic,strong) UIDynamicAnimator *animator; //@property(nonatomic,strong) UIAttachmentBehavior *attachmentBehavior; @property(nonatomic) CGPoint attachPoint; @property(nonatomic,strong) MaskTimeCircularProgressView *cicleProgressView; @end @implementation GiGaMaskTaskViewController //提示label - (UILabel *)descLabl{ if(!_descLabl){ _descLabl = [[UILabel alloc] init]; _descLabl.text = @"请点击或按住面膜上滑敷到脸部开始倒计时"; _descLabl.textAlignment = NSTextAlignmentCenter; _descLabl.numberOfLines = 2; _descLabl.textColor = GIGARGB(89, 216, 197, 1); _descLabl.font = [UIFont fontWithName:GIGA_FONTNAME size:11]; } return _descLabl; } - (UIView *)descBackView{ if(!_descBackView){ _descBackView = [[UIView alloc] init]; _descBackView.layer.borderWidth = 1; _descBackView.layer.borderColor = GIGARGB(120, 217, 202, 1).CGColor; _descBackView.layer.masksToBounds = YES; _descBackView.layer.cornerRadius = 1; } return _descBackView; } //面膜 - (UIImageView *)maskImageView{ if (!_maskImageView) { _maskImageView = [[UIImageView alloc] init]; _maskImageView.userInteractionEnabled = YES; //_maskImageView.backgroundColor = [UIColor purpleColor]; _maskImageView.image = [UIImage imageNamed:@"img_bg"]; UIPanGestureRecognizer *pangesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)]; [_maskImageView addGestureRecognizer:pangesture]; } return _maskImageView; } //人脸 - (UIImageView *)faceView{ if (!_faceView) { _faceView = [[UIImageView alloc] init]; //_faceView.backgroundColor = [UIColor lightGrayColor]; _faceView.userInteractionEnabled = YES; _faceView.image = [UIImage imageNamed:@"img_bg_head"]; } return _faceView; } //倒计时圆环 - (MaskTimeCircularProgressView *)cicleProgressView{ if (!_cicleProgressView) { _cicleProgressView = [[MaskTimeCircularProgressView alloc] initWithFrame:CGRectMake(100, 500, 136, 136) backColor:GIGARGB(22, 22, 22, 1) progressColor:[UIColor blueColor] lineWidth:10 timeinterval:10]; _cicleProgressView.delegate = self; _cicleProgressView.hidden = YES; } return _cicleProgressView; } //底部提示栏 - (MaskViewBootomWaringView *)waringView{ if (!_waringView) { self.waringView = [[MaskViewBootomWaringView alloc] init]; self.waringView.frame = CGRectMake(0,CGRectGetMaxY(self.view.frame), self.view.frame.size.width, 60); } return _waringView; } //弹幕 -(void)createInputView{ GiGaFlingCommitVC* GiGaFlingVC = [[GiGaFlingCommitVC alloc] init]; if (ISIOS8) { GiGaFlingVC.modalPresentationStyle = UIModalPresentationOverCurrentContext; }else{ GiGaFlingVC.modalPresentationStyle = UIModalPresentationCurrentContext; } GiGaFlingVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; [self presentViewController:GiGaFlingVC animated:NO completion:nil]; } //滑动手势 -(void)panGestureAction:(UIPanGestureRecognizer *)pan{ CGPoint panLocation = [pan locationInView:self.view]; NSLog(@"panlocation.y:%f",panLocation.y); if (pan.state == UIGestureRecognizerStateBegan) { [self animaMask]; }else if (pan.state == UIGestureRecognizerStateChanged){ }else if (pan.state == UIGestureRecognizerStateEnded){ }else{ } } #pragma mark -开始面膜时间动画 -(void)animaMask{ self.descLabl.hidden = YES; self.descBackView.hidden = YES; [self.view layoutIfNeeded]; [UIView animateWithDuration:1.0 animations:^{ [self.maskImageView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.attachPoint.y - 118); }]; [self.faceView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.attachPoint.y -174); }]; [self.view layoutIfNeeded]; } completion:^(BOOL finished) { self.cicleProgressView.hidden = NO; self.maskImageView.userInteractionEnabled = NO; //开始面膜时间 [self startMaskTime]; }]; } #pragma mark 面膜时间结束复原动画 -(void)endMaskAnimation{ self.cicleProgressView.hidden = YES; self.descLabl.hidden = NO; self.descBackView.hidden = NO; [self.view layoutIfNeeded]; [UIView animateWithDuration:1.0 animations:^{ [self.maskImageView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(314 + PhoneX_TopMargin); }]; [self.faceView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(103 + PhoneX_TopMargin); }]; [self.view layoutIfNeeded]; } completion:^(BOOL finished) { self.maskImageView.userInteractionEnabled = YES; }]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.navigationController.interactivePopGestureRecognizer.enabled = NO; } BOOL showAppGaurd = [GiGaUserDefault isShowedAppGaurd]; if (showAppGaurd == NO) { [self showAPPappGaurdView]; } } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.navigationController.interactivePopGestureRecognizer.enabled = YES; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setUpNav]; [self initUI]; [self addNotify]; [self creatBackGroundMusic]; } -(void)initUI{ self.view.backgroundColor = GIGARGB(56, 56, 56, 1); [self.view addSubview:self.maskImageView]; [self.view addSubview:self.faceView]; [self.view bringSubviewToFront:self.maskImageView]; [self.view addSubview:self.descBackView]; [self.view addSubview:self.cicleProgressView]; //face [self.faceView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.view.mas_top).offset(103 + PhoneX_TopMargin); make.left.mas_equalTo(self.view.mas_centerX).offset(-69); make.size.mas_equalTo(CGSizeMake(138, 174)); }]; //mask [self.maskImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.view.mas_top).offset(314 + PhoneX_TopMargin); make.left.mas_equalTo(self.view.mas_centerX).offset(-51); make.size.mas_equalTo(CGSizeMake(102, 118)); }]; //描述 [self.descBackView mas_makeConstraints:^(MASConstraintMaker *make){ make.top.mas_equalTo(500 + PhoneX_TopMargin); make.width.mas_equalTo(149); make.height.mas_equalTo(45); make.centerX.mas_equalTo(self.view.mas_centerX); }]; [self.descBackView addSubview:self.descLabl]; [self.descLabl mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(121); make.height.mas_equalTo(32); make.center.mas_equalTo(self.descBackView.center); }]; // UIButton *testBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // [testBtn setTitle:@"测试肤质" forState:UIControlStateNormal]; // [testBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // testBtn.backgroundColor = [UIColor blueColor]; // [testBtn addTarget:self action:@selector(testBtnAcion:) forControlEvents:UIControlEventTouchUpInside]; // testBtn.layer.masksToBounds = YES; // testBtn.layer.cornerRadius = 4; // [self.view addSubview:testBtn]; // // [testBtn mas_makeConstraints:^(MASConstraintMaker *make) { // make.centerX.mas_equalTo(self.view.mas_centerX); // make.width.mas_equalTo(150); // make.height.mas_equalTo(40); // make.bottom.mas_equalTo(self.view.mas_bottom).offset(-130); // }]; [self.view layoutIfNeeded]; CGFloat Y = CGRectGetMaxY(self.faceView.frame) + (CGRectGetMaxY(self.maskImageView.frame) - CGRectGetMaxY(self.faceView.frame)) / 2; CGFloat X = CGRectGetMidX(self.view.frame); // 下巴合并位置 CGPoint anchorPoint = CGPointMake(X, Y); self.attachPoint = anchorPoint; [self.cicleProgressView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(Y + 68); make.height.and.width.mas_equalTo(136); make.centerX.mas_equalTo(self.view.mas_centerX); }]; } #pragma mark 面膜时间背景音乐 -(void)creatBackGroundMusic{ //初始化音频播放器 NSError *payerError; NSURL *musicUrl = [[NSURL alloc] initWithString:[[NSBundle mainBundle] pathForResource:@"pomodoSound" ofType:@"m4a"]] ; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:musicUrl error:&payerError]; player.numberOfLoops = -1; _musicPalyer = player; [[AVAudioSession sharedInstance] setActive:YES error:nil]; //设置后台模式和锁屏模式下依然能够播放 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; } #pragma mark 开始面膜时间 -(void)startMaskTime{ //[self createInputView]; //出现弹幕 AppDelegate *delegate =(AppDelegate *) [[UIApplication sharedApplication] delegate]; if (delegate.isMasking) { return; } delegate.isMasking = YES; [_musicPalyer play]; [self setBackGroundPlayingInfo];//锁屏音频信息 //总时间隔 s NSTimeInterval timeInterVal = 10; [self.cicleProgressView startWith:timeInterVal]; //底部弹窗 [self showWaringView]; //app 前台时收到本地通知showAlert 后台时通知提醒 [[GiGaLocalNotificationManager localNotifiationCenter] sendLocalNotification:@"本地推送" fireTimeInterval:timeInterVal alertAction:@"测试面膜时间" withIdentifier:kLOCALNotifiID_MASKETIMEEND]; } #pragma mark 设置后台锁屏音频信息 -(void)setBackGroundPlayingInfo{ MPMediaItemArtwork *arwork = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"MaskTime.png"]]; NSDictionary *dic = @{ MPMediaItemPropertyTitle:@"面膜时间", MPMediaItemPropertyAlbumArtist:@"专辑专辑专辑专辑", MPMediaItemPropertyArtist:@"lianxiang", MPMediaItemPropertyAlbumTitle:@"hahah", MPMediaItemPropertyGenre:@"???", MPMediaItemPropertyLyrics:@"歌词", MPMediaItemPropertyPodcastTitle:@"PodcastTitle", MPMediaItemPropertyArtwork:arwork }; [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dic]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; } - (void)remoteControlReceivedWithEvent: (UIEvent *) receivedEvent { if (receivedEvent.type == UIEventTypeRemoteControl) { switch (receivedEvent.subtype) { case UIEventSubtypeRemoteControlPlay: //暂时不处理 //[_musicPalyer play]; break; case UIEventSubtypeRemoteControlPause: // [_musicPalyer pause]; break; default: NSLog(@"没有处理过这个事件------receivedEvent.subtype==%ld",(long)receivedEvent.subtype); break; } } } #pragma mark 面膜时间结束 -(void)maskTimeEnd{ NC_POST_NAME_OBJECT(kUserNoti_MASKEND, nil); //面膜时间结束时设置 全局变量 不再发送本地通知 提醒 AppDelegate *delegate =(AppDelegate *) [[UIApplication sharedApplication] delegate]; delegate.isMasking = NO; [_musicPalyer stop]; //复原动画 [self endMaskAnimation]; } #pragma mark 开始测试 -(void)testBtnAcion:(UIButton *)btn{ BOOL isUserLogin = [GiGaUserDefault isUserLogin]; if (isUserLogin) { GiGaQuestionVC *questionVC = [[GiGaQuestionVC alloc] init]; [self.navigationController pushViewController:questionVC animated:YES]; }else{ GiGaUserLoginVC *userlogInVC= [[GiGaUserLoginVC alloc] init]; GiGaBaseNavViewController *baseNav = [[GiGaBaseNavViewController alloc] initWithRootViewController:userlogInVC]; [self presentViewController:baseNav animated:YES completion:nil]; } } -(void)addNotify{ NC_ADD_TARGET_NAME_OBJECT(self, @selector(guardViewDissmiss), USER_GUARD_DISSMISS, nil) ; NC_ADD_TARGET_NAME_OBJECT(self, @selector(appGauardViewDissmiss),APP_GUARD_DISSMISS, nil) ; NC_ADD_TARGET_NAME_OBJECT(self, @selector(pushToAppAdDetailView),APP_PUSHTO_DETAILVIEW, nil) ; } #pragma mark 广告详情 -(void)pushToAppAdDetailView{ GiGaAdDetailViewController *detailVC=[[GiGaAdDetailViewController alloc] init]; [self.navigationController pushViewController:detailVC animated:YES]; } #pragma mark 新手引导消失 -(void)guardViewDissmiss{ [self showWaringView]; } #pragma mark app引导页消失 -(void)appGauardViewDissmiss{ //出现新手引导 BOOL isshowGaurd = [GiGaUserDefault isShowedGaurd]; if (isshowGaurd == NO) { NSLog(@"%d",(int)isshowGaurd ); [self showUserGaurdView]; } } -(void)dealloc{ NC_REMOVE_NAME(self, USER_GUARD_DISSMISS, nil); NC_REMOVE_NAME(self, APP_GUARD_DISSMISS, nil); NC_REMOVE_NAME(self, APP_PUSHTO_DETAILVIEW, nil); } #pragma mark 新手引导 -(void)showUserGaurdView{ UserGuardViewController *userGuardVC = [[UserGuardViewController alloc] init]; if (ISIOS8) { userGuardVC.modalPresentationStyle = UIModalPresentationOverCurrentContext; }else{ userGuardVC.modalPresentationStyle = UIModalPresentationCurrentContext; } userGuardVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; [self presentViewController:userGuardVC animated:NO completion:nil]; } #pragma mark App引导页 -(void)showAPPappGaurdView{ GiGaAppGaurdVC *gaurdVC = [[GiGaAppGaurdVC alloc] init]; [self presentViewController:gaurdVC animated:YES completion:nil]; } #pragma mark - setUpNav -(void)setUpNav{ //leftItem UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; leftBtn.frame = CGRectMake(0, 0, 40, 40); leftBtn.backgroundColor = [UIColor redColor]; [leftBtn setImage:[UIImage imageNamed:@"btn_back"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn]; //rightItem UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; rightBtn.frame = CGRectMake(0, 0, 40, 40); rightBtn.backgroundColor = [UIColor blueColor]; [rightBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; [rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn]; [self.navigationController.navigationBar cnSetBackgroundColor:GIGARGB(216, 216, 216, 1)]; } #pragma mark 消息 action -(void)leftBtnAction{ BOOL isUserLogin = [GiGaUserDefault isUserLogin]; if (isUserLogin) { GiGaMasssagesVC *masageVC= [[GiGaMasssagesVC alloc] init]; [self.navigationController pushViewController:masageVC animated:YES]; }else{ GiGaUserLoginVC *userlogInVC= [[GiGaUserLoginVC alloc] init]; GiGaBaseNavViewController *baseNav = [[GiGaBaseNavViewController alloc] initWithRootViewController:userlogInVC]; [self presentViewController:baseNav animated:YES completion:nil]; } } #pragma mark 个人中心 action -(void)rightBtnAction{ GiGaMeViewController *meVC= [[GiGaMeViewController alloc] init]; [self.navigationController pushViewController:meVC animated:YES]; } #pragma mark -bottomView -(void)showWaringView{ [self.view addSubview:self.waringView]; [UIView animateWithDuration:0.4 animations:^{ self.waringView.frame = CGRectMake(0,CGRectGetMaxY(self.view.frame) - 60, self.view.frame.size.width, 60); }]; [self.waringView showView]; } #pragma mark - MaskCirCularProGressViewDelegate - (void)updateProgressViewWith:(NSTimeInterval)timeInterval{ } - (void)progressDidFish{ NSLog(@"结束"); [self maskTimeEnd]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end