// // HomeSCDetailVC.m // PersonalCenter // // Created by Arch on 2017/6/16. // Copyright © 2017年 mint_bin. All rights reserved. // #import "HomeSCDetailVC.h" #import "SegmentView.h" #import "FirstViewController.h" #import "ThirdViewController.h" #import "SecondViewController.h" #import "HomeSubItemModel.h" #import "CenterTouchTableView.h" #import "HomeSchoolHeadView.h" #import "MoveViewController.h" #import "HomeSchollVideoVC.h" @interface HomeSCDetailVC () @property (nonatomic, strong) CenterTouchTableView *mainTableView; @property (nonatomic, strong) SegmentView *segmentView; @property (nonatomic, strong) UIView *naviView; @property (nonatomic, strong) UIImageView *headerImageView; @property (nonatomic, strong) HomeSchoolHeadView *headerContentView; /** mainTableView是否可以滚动 */ @property (nonatomic, assign) BOOL canScroll; /** segmentHeaderView到达顶部, mainTableView不能移动 */ @property (nonatomic, assign) BOOL isTopIsCanNotMoveTabView; /** segmentHeaderView离开顶部,childViewController的滚动视图不能移动 */ @property (nonatomic, assign) BOOL isTopIsCanNotMoveTabViewPre; /** 是否正在pop */ @property (nonatomic, assign) BOOL isBacking; @property (nonatomic, strong) HomeSubItemModel *model; @property (nonatomic, strong) UIButton *backBtn; @property (nonatomic, strong) UIButton *menuBtn; @property (nonatomic, strong) UIButton *shareBtn; @property (nonatomic, assign) CGFloat HeaderImageViewHeight; @property (nonatomic, strong) FirstViewController *firstVC; @property (nonatomic, strong) SecondViewController *secondVC; @property (nonatomic, strong) ThirdViewController *thirdVC; @end @implementation HomeSCDetailVC - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.HeaderImageViewHeight = SCREEN_WIDTH / 375 * 183 + 40.f; [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever]; //如果使用自定义的按钮去替换系统默认返回按钮,会出现滑动返回手势失效的情况 self.navigationController.interactivePopGestureRecognizer.delegate = self; [self setupSubViews]; //注册允许外层tableView滚动通知-解决和分页视图的上下滑动冲突问题 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:@"leaveTop" object:nil]; //分页的scrollView左右滑动的时候禁止mainTableView滑动,停止滑动的时候允许mainTableView滑动 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:IsEnablePersonalCenterVCMainTableViewScroll object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collectSuccess) name:COLLECTSUCCESS object:nil]; [self getData]; WS(weakSelf); [self.headerContentView.collectBtn setAction:^{ if (weakSelf.model.IsCollect) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"真的取消收藏吗?" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSMutableDictionary *dic = [[NSMutableDictionary alloc]init]; [dic setValue:@(weakSelf.Id) forKey:@"CollectionDataId"]; /// 1文章 2话题 3 收藏 4笔记 5通知 6站内信 7小组 8 会议详情 14工作流审批 300 文件 400 会议纪要 [dic setValue:@(CollectModel_Organization) forKey:@"CollectionType"]; [dic setValue:@(0) forKey:@"FolderId"]; [dic setValue:@(0) forKey:@"SourceUserId"]; [[HttpManager sharedHttpManager] PUTUrl:Host(API_CreateCollect) parameters:dic success:^(id _Nonnull responseObject) { weakSelf.model.IsCollect = NO; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.headerContentView setDataWithModel:weakSelf.model]; }); } failure:^(NSError * _Nonnull error) { // SHOWERROR([ZYCTool handerResultData:error]); }]; }]; UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:actionYes]; [alert addAction:actionNo]; [weakSelf presentViewController:alert animated:YES completion:^{ }]; }); }else{ [weakSelf gotoCollectHander:weakSelf.Id]; } }]; } - (void)gotoCollectHander:(NSInteger)Id{ MoveViewController *vc = [MoveViewController initMoveViewController]; vc.CollectionDataId = Id; vc.collectType = CollectHanderType_Collect; vc.CollectionType = CollectModel_Organization; vc.ParentId = 0; vc.titleStr = @"我的收藏"; vc.TypeId = CreateCollectionType; vc.FolderIds = @[].mutableCopy; [self.navigationController pushViewController:vc animated:YES]; } - (void)collectSuccess { self.model.IsCollect = YES; [self.headerContentView setDataWithModel:self.model]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES]; self.naviView.hidden = NO; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.isBacking = NO; [[NSNotificationCenter defaultCenter] postNotificationName:PersonalCenterVCBackingStatus object:nil userInfo:@{@"isBacking" : @(self.isBacking)}]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.isBacking = YES; [[NSNotificationCenter defaultCenter] postNotificationName:PersonalCenterVCBackingStatus object:nil userInfo:@{@"isBacking" : @(self.isBacking)}]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; self.fd_prefersNavigationBarHidden = YES; [self.navigationController setNavigationBarHidden:YES]; self.naviView.hidden = YES; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - Private Methods - (void)setupSubViews { [self.view addSubview:self.mainTableView]; [self.view addSubview:self.naviView]; [self.headerImageView addSubview:self.headerContentView]; [self.mainTableView addSubview:self.headerImageView]; [self.headerContentView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.centerX.mas_equalTo(self.headerImageView); make.width.mas_equalTo(SCREEN_WIDTH); make.height.mas_equalTo(self.HeaderImageViewHeight); }]; [self.naviView addSubview:self.backBtn]; [self.naviView addSubview:self.menuBtn]; [self.naviView addSubview:self.shareBtn]; [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.mas_equalTo(self.naviView); make.size.mas_offset(CGSizeMake(44, 44)); }]; [self.menuBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(self.naviView); make.left.mas_equalTo(self.backBtn.mas_right); make.size.mas_offset(CGSizeMake(44, 44)); }]; [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.bottom.mas_equalTo(self.naviView); make.size.mas_offset(CGSizeMake(44, 44)); }]; [self.headerContentView.playBtn addTarget:self action:@selector(PlayAction) forControlEvents:UIControlEventTouchUpInside]; } - (void)PlayAction { if (self.model.FileUrl) { HomeSchollVideoVC * vc = [HomeSchollVideoVC initHomeSchollVideoVC]; vc.file = self.model.FileUrl; vc.VideoImage = self.model.VideoCoverImage ? self.model.VideoCoverImage : self.model.VideoImage; [self.navigationController pushViewController:vc animated:YES]; }else{ SHOWERROR(@"没有视频地址"); } } - (void)getData { WS(weakSelf); NSString * url = [NSString stringWithFormat:@"%@%ld",Host(API_APP_Press),self.Id]; [[HttpManager sharedHttpManager] GETUrl:url parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) { NSLog(@"%@",responseObject); if ([responseObject isKindOfClass:[NSDictionary class]]) { weakSelf.model = [HomeSubItemModel modelWithDictionary:responseObject]; [weakSelf.headerContentView setDataWithModel:weakSelf.model]; [weakSelf.firstVC setDataText:weakSelf.model.Summary]; [weakSelf.secondVC setDataArray:weakSelf.model.TeacherResult]; [weakSelf.thirdVC setDataArray:weakSelf.model.Resource]; } } failure:^(NSError * _Nonnull error) { }]; } #pragma mark - Notification - (void)acceptMsg:(NSNotification *)notification { NSDictionary *userInfo = notification.userInfo; if ([notification.name isEqualToString:@"leaveTop"]) { NSString *canScroll = userInfo[@"canScroll"]; if ([canScroll isEqualToString:@"1"]) { self.canScroll = YES; } } else if ([notification.name isEqualToString:IsEnablePersonalCenterVCMainTableViewScroll]) { NSString *canScroll = userInfo[@"canScroll"]; if ([canScroll isEqualToString:@"1"]) { self.mainTableView.scrollEnabled = YES; }else if([canScroll isEqualToString:@"0"]) { self.mainTableView.scrollEnabled = NO; } } } #pragma mark - UiScrollViewDelegate /** * 处理联动 * 因为要实现下拉头部放大的问题,tableView设置了contentInset,所以试图刚加载的时候会调用一遍这个方法,所以要做一些特殊处理, */ - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == self.mainTableView) { //当前y轴偏移量 CGFloat yOffset = scrollView.contentOffset.y; //临界点偏移量(吸顶临界点) CGFloat tabyOffset = [self.mainTableView rectForSection:0].origin.y - STATUS_BAR_HEIGHT - NAVIGATION_BAR_HEIGHT; //第一部分: 更改导航栏的背景图的透明度 CGFloat alpha = 0; if (-yOffset <= STATUS_BAR_HEIGHT + NAVIGATION_BAR_HEIGHT) { alpha = 1; } else if ((-yOffset > STATUS_BAR_HEIGHT + NAVIGATION_BAR_HEIGHT) && -yOffset < self.HeaderImageViewHeight) { alpha = (self.HeaderImageViewHeight + yOffset) / (self.HeaderImageViewHeight - STATUS_BAR_HEIGHT - NAVIGATION_BAR_HEIGHT); }else { alpha = 0; } //第二部分: //利用contentOffset处理内外层scrollView的滑动冲突问题 if (yOffset >= tabyOffset) { scrollView.contentOffset = CGPointMake(0, tabyOffset); _isTopIsCanNotMoveTabView = YES; }else{ _isTopIsCanNotMoveTabView = NO; } _isTopIsCanNotMoveTabViewPre = !_isTopIsCanNotMoveTabView; if (!_isTopIsCanNotMoveTabViewPre) { [[NSNotificationCenter defaultCenter] postNotificationName:@"goTop" object:nil userInfo:@{@"canScroll":@"1"}]; _canScroll = NO; } else{ if (!_canScroll) { _mainTableView.contentOffset = CGPointMake(0, tabyOffset); } } //第三部分: /** * 处理头部自定义背景视图 (如: 下拉放大) * 图片会被拉伸多出状态栏的高度 */ if(yOffset <= -self.HeaderImageViewHeight) { if (_isEnlarge) { CGRect f = self.headerImageView.frame; //改变HeadImageView的frame //上下放大 f.origin.y = yOffset; f.size.height = -yOffset; //左右放大 f.origin.x = (yOffset * SCREEN_WIDTH / self.HeaderImageViewHeight + SCREEN_WIDTH) / 2; f.size.width = -yOffset * SCREEN_WIDTH / self.HeaderImageViewHeight; //改变头部视图的frame self.headerImageView.frame = f; }else{ scrollView.bounces = NO; } }else { scrollView.bounces = YES; } } } #pragma mark - UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return SCREEN_HEIGHT - STATUS_BAR_HEIGHT - NAVIGATION_BAR_HEIGHT; } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell.contentView addSubview:self.setPageViewControllers]; return cell; } - (UITableView *)mainTableView { if (!_mainTableView) { //⚠️这里的属性初始化一定要放在mainTableView.contentInset的设置滚动之前, 不然首次进来视图就会偏移到临界位置,contentInset会调用scrollViewDidScroll这个方法。 //初始化变量 self.canScroll = YES; self.isTopIsCanNotMoveTabView = NO; self.mainTableView = [[CenterTouchTableView alloc]initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT) style:UITableViewStylePlain]; _mainTableView.delegate = self; _mainTableView.dataSource = self; _mainTableView.showsVerticalScrollIndicator = NO; //注意:这里不能使用动态高度_headimageHeight, 不然tableView会往下移,在iphone X下,头部不放大的时候,上方依然会有白色空白 _mainTableView.contentInset = UIEdgeInsetsMake(self.HeaderImageViewHeight, 0, 0, 0);//内容视图开始正常显示的坐标为(0, self.HeaderImageViewHeight) } return _mainTableView; } - (HomeSchoolHeadView *)headerContentView { if (!_headerContentView) { _headerContentView = [[HomeSchoolHeadView alloc]init]; } return _headerContentView; } - (UIImageView *)headerImageView { if (!_headerImageView) { _headerImageView = [[UIImageView alloc] init]; _headerImageView.backgroundColor = [UIColor clearColor]; _headerImageView.userInteractionEnabled = YES; _headerImageView.frame = CGRectMake(0, -self.HeaderImageViewHeight, SCREEN_WIDTH, self.HeaderImageViewHeight); } return _headerImageView; } /* * 这里可以设置替换你喜欢的segmentView */ - (UIView *)setPageViewControllers { if (!_segmentView) { //设置子控制器 self.firstVC = [[FirstViewController alloc] init]; self.secondVC = [[SecondViewController alloc] init]; self.thirdVC = [[ThirdViewController alloc] init]; NSArray *controllers = @[self.firstVC, self.secondVC, self.thirdVC]; NSArray *titleArray = @[@"概况", @"人物", @"共享资源"]; SegmentView *segmentView = [[SegmentView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT - NAVIGATION_BAR_HEIGHT) controllers:controllers titleArray:(NSArray *)titleArray parentController:self]; //注意:不能通过初始化方法传递selectedIndex的初始值,因为内部使用的是Masonry布局的方式, 否则设置selectedIndex不起作用 segmentView.selectedIndex = self.selectedIndex; _segmentView = segmentView; } return _segmentView; } - (UIView *)naviView { if (!_naviView) { _naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, STATUS_BAR_HEIGHT + NAVIGATION_BAR_HEIGHT)]; _naviView.backgroundColor = [UIColor colorWithWhite:1 alpha:0]; } return _naviView; } - (UIButton *)backBtn { if (!_backBtn) { _backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)]; [_backBtn setImage:[UIImage imageNamed:@"back_white_icon"] forState:(UIControlStateNormal)]; _backBtn.frame = CGRectMake(0,STATUS_BAR_HEIGHT, 44, 44); _backBtn.adjustsImageWhenHighlighted = YES; [_backBtn addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)]; } return _backBtn; } - (UIButton *)menuBtn { if (!_menuBtn) { _menuBtn = [UIButton buttonWithType:(UIButtonTypeCustom)]; [_menuBtn setImage:[UIImage imageNamed:@"menu_white_icon"] forState:(UIControlStateNormal)]; _menuBtn.adjustsImageWhenHighlighted = YES; [_menuBtn addTarget:self action:@selector(menuAction) forControlEvents:(UIControlEventTouchUpInside)]; } return _menuBtn; } - (UIButton *)shareBtn { if (!_shareBtn) { _shareBtn = [UIButton buttonWithType:(UIButtonTypeCustom)]; [_shareBtn setImage:[UIImage imageNamed:@"shareWhile"] forState:(UIControlStateNormal)]; _shareBtn.adjustsImageWhenHighlighted = YES; [_shareBtn addTarget:self action:@selector(shareAction) forControlEvents:(UIControlEventTouchUpInside)]; } return _shareBtn; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)menuAction { // [super menuAction:nil]; } - (void)shareAction { } @end