HomeSCDetailVC.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // HomeSCDetailVC.m
  3. // PersonalCenter
  4. //
  5. // Created by Arch on 2017/6/16.
  6. // Copyright © 2017年 mint_bin. All rights reserved.
  7. //
  8. #import "HomeSCDetailVC.h"
  9. #import "SegmentView.h"
  10. #import "FirstViewController.h"
  11. #import "ThirdViewController.h"
  12. #import "SecondViewController.h"
  13. #import "HomeSubItemModel.h"
  14. #import "CenterTouchTableView.h"
  15. #import "HomeSchoolHeadView.h"
  16. #import "MoveViewController.h"
  17. #import "HomeSchollVideoVC.h"
  18. @interface HomeSCDetailVC () <UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>
  19. @property (nonatomic, strong) CenterTouchTableView *mainTableView;
  20. @property (nonatomic, strong) SegmentView *segmentView;
  21. @property (nonatomic, strong) UIView *naviView;
  22. @property (nonatomic, strong) UIImageView *headerImageView;
  23. @property (nonatomic, strong) HomeSchoolHeadView *headerContentView;
  24. /** mainTableView是否可以滚动 */
  25. @property (nonatomic, assign) BOOL canScroll;
  26. /** segmentHeaderView到达顶部, mainTableView不能移动 */
  27. @property (nonatomic, assign) BOOL isTopIsCanNotMoveTabView;
  28. /** segmentHeaderView离开顶部,childViewController的滚动视图不能移动 */
  29. @property (nonatomic, assign) BOOL isTopIsCanNotMoveTabViewPre;
  30. /** 是否正在pop */
  31. @property (nonatomic, assign) BOOL isBacking;
  32. @property (nonatomic, strong) HomeSubItemModel *model;
  33. @property (nonatomic, strong) UIButton *backBtn;
  34. @property (nonatomic, strong) UIButton *menuBtn;
  35. @property (nonatomic, strong) UIButton *shareBtn;
  36. @property (nonatomic, assign) CGFloat HeaderImageViewHeight;
  37. @property (nonatomic, strong) FirstViewController *firstVC;
  38. @property (nonatomic, strong) SecondViewController *secondVC;
  39. @property (nonatomic, strong) ThirdViewController *thirdVC;
  40. @end
  41. @implementation HomeSCDetailVC
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. self.view.backgroundColor = [UIColor whiteColor];
  45. self.HeaderImageViewHeight = SCREEN_WIDTH / 375 * 183 + 40.f;
  46. [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
  47. //如果使用自定义的按钮去替换系统默认返回按钮,会出现滑动返回手势失效的情况
  48. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  49. [self setupSubViews];
  50. //注册允许外层tableView滚动通知-解决和分页视图的上下滑动冲突问题
  51. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:@"leaveTop" object:nil];
  52. //分页的scrollView左右滑动的时候禁止mainTableView滑动,停止滑动的时候允许mainTableView滑动
  53. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:IsEnablePersonalCenterVCMainTableViewScroll object:nil];
  54. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collectSuccess) name:COLLECTSUCCESS object:nil];
  55. [self getData];
  56. WS(weakSelf);
  57. [self.headerContentView.collectBtn setAction:^{
  58. if (weakSelf.model.IsCollect) {
  59. dispatch_async(dispatch_get_main_queue(), ^{
  60. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"真的取消收藏吗?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  61. UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  62. NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
  63. [dic setValue:@(weakSelf.Id) forKey:@"CollectionDataId"];
  64. /// 1文章 2话题 3 收藏 4笔记 5通知 6站内信 7小组 8 会议详情 14工作流审批 300 文件 400 会议纪要
  65. [dic setValue:@(CollectModel_Organization) forKey:@"CollectionType"];
  66. [dic setValue:@(0) forKey:@"FolderId"];
  67. [dic setValue:@(0) forKey:@"SourceUserId"];
  68. [[HttpManager sharedHttpManager] PUTUrl:Host(API_CreateCollect) parameters:dic success:^(id _Nonnull responseObject) {
  69. weakSelf.model.IsCollect = NO;
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. [weakSelf.headerContentView setDataWithModel:weakSelf.model];
  72. });
  73. } failure:^(NSError * _Nonnull error) {
  74. // SHOWERROR([ZYCTool handerResultData:error]);
  75. }];
  76. }];
  77. UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  78. }];
  79. [alert addAction:actionYes];
  80. [alert addAction:actionNo];
  81. [weakSelf presentViewController:alert animated:YES completion:^{
  82. }];
  83. });
  84. }else{
  85. [weakSelf gotoCollectHander:weakSelf.Id];
  86. }
  87. }];
  88. }
  89. - (void)gotoCollectHander:(NSInteger)Id{
  90. MoveViewController *vc = [MoveViewController initMoveViewController];
  91. vc.CollectionDataId = Id;
  92. vc.collectType = CollectHanderType_Collect;
  93. vc.CollectionType = CollectModel_Organization;
  94. vc.ParentId = 0;
  95. vc.titleStr = @"我的收藏";
  96. vc.TypeId = CreateCollectionType;
  97. vc.FolderIds = @[].mutableCopy;
  98. [self.navigationController pushViewController:vc animated:YES];
  99. }
  100. - (void)collectSuccess
  101. {
  102. self.model.IsCollect = YES;
  103. [self.headerContentView setDataWithModel:self.model];
  104. }
  105. - (void)viewWillAppear:(BOOL)animated {
  106. [super viewWillAppear:animated];
  107. [self.navigationController setNavigationBarHidden:YES];
  108. self.naviView.hidden = NO;
  109. }
  110. - (void)viewDidAppear:(BOOL)animated {
  111. [super viewDidAppear:animated];
  112. self.isBacking = NO;
  113. [[NSNotificationCenter defaultCenter] postNotificationName:PersonalCenterVCBackingStatus object:nil userInfo:@{@"isBacking" : @(self.isBacking)}];
  114. }
  115. - (void)viewWillDisappear:(BOOL)animated {
  116. [super viewWillDisappear:animated];
  117. self.isBacking = YES;
  118. [[NSNotificationCenter defaultCenter] postNotificationName:PersonalCenterVCBackingStatus object:nil userInfo:@{@"isBacking" : @(self.isBacking)}];
  119. }
  120. - (void)viewDidDisappear:(BOOL)animated {
  121. [super viewDidDisappear:animated];
  122. self.fd_prefersNavigationBarHidden = YES;
  123. [self.navigationController setNavigationBarHidden:YES];
  124. self.naviView.hidden = YES;
  125. }
  126. - (void)dealloc {
  127. [[NSNotificationCenter defaultCenter] removeObserver:self];
  128. }
  129. #pragma mark - Private Methods
  130. - (void)setupSubViews {
  131. [self.view addSubview:self.mainTableView];
  132. [self.view addSubview:self.naviView];
  133. [self.headerImageView addSubview:self.headerContentView];
  134. [self.mainTableView addSubview:self.headerImageView];
  135. [self.headerContentView mas_makeConstraints:^(MASConstraintMaker *make) {
  136. make.bottom.centerX.mas_equalTo(self.headerImageView);
  137. make.width.mas_equalTo(SCREEN_WIDTH);
  138. make.height.mas_equalTo(self.HeaderImageViewHeight);
  139. }];
  140. [self.naviView addSubview:self.backBtn];
  141. [self.naviView addSubview:self.menuBtn];
  142. [self.naviView addSubview:self.shareBtn];
  143. [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  144. make.left.bottom.mas_equalTo(self.naviView);
  145. make.size.mas_offset(CGSizeMake(44, 44));
  146. }];
  147. [self.menuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  148. make.bottom.mas_equalTo(self.naviView);
  149. make.left.mas_equalTo(self.backBtn.mas_right);
  150. make.size.mas_offset(CGSizeMake(44, 44));
  151. }];
  152. [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  153. make.right.bottom.mas_equalTo(self.naviView);
  154. make.size.mas_offset(CGSizeMake(44, 44));
  155. }];
  156. [self.headerContentView.playBtn addTarget:self action:@selector(PlayAction) forControlEvents:UIControlEventTouchUpInside];
  157. }
  158. - (void)PlayAction
  159. {
  160. if (self.model.FileUrl) {
  161. HomeSchollVideoVC * vc = [HomeSchollVideoVC initHomeSchollVideoVC];
  162. vc.file = self.model.FileUrl;
  163. vc.VideoImage = self.model.VideoCoverImage ? self.model.VideoCoverImage : self.model.VideoImage;
  164. [self.navigationController pushViewController:vc animated:YES];
  165. }else{
  166. SHOWERROR(@"没有视频地址");
  167. }
  168. }
  169. - (void)getData
  170. {
  171. WS(weakSelf);
  172. NSString * url = [NSString stringWithFormat:@"%@%ld",Host(API_APP_Press),self.Id];
  173. [[HttpManager sharedHttpManager] GETUrl:url parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  174. NSLog(@"%@",responseObject);
  175. if ([responseObject isKindOfClass:[NSDictionary class]]) {
  176. weakSelf.model = [HomeSubItemModel modelWithDictionary:responseObject];
  177. [weakSelf.headerContentView setDataWithModel:weakSelf.model];
  178. [weakSelf.firstVC setDataText:weakSelf.model.Summary];
  179. [weakSelf.secondVC setDataArray:weakSelf.model.TeacherResult];
  180. [weakSelf.thirdVC setDataArray:weakSelf.model.Resource];
  181. }
  182. } failure:^(NSError * _Nonnull error) {
  183. }];
  184. }
  185. #pragma mark - Notification
  186. - (void)acceptMsg:(NSNotification *)notification {
  187. NSDictionary *userInfo = notification.userInfo;
  188. if ([notification.name isEqualToString:@"leaveTop"]) {
  189. NSString *canScroll = userInfo[@"canScroll"];
  190. if ([canScroll isEqualToString:@"1"]) {
  191. self.canScroll = YES;
  192. }
  193. } else if ([notification.name isEqualToString:IsEnablePersonalCenterVCMainTableViewScroll]) {
  194. NSString *canScroll = userInfo[@"canScroll"];
  195. if ([canScroll isEqualToString:@"1"]) {
  196. self.mainTableView.scrollEnabled = YES;
  197. }else if([canScroll isEqualToString:@"0"]) {
  198. self.mainTableView.scrollEnabled = NO;
  199. }
  200. }
  201. }
  202. #pragma mark - UiScrollViewDelegate
  203. /**
  204. * 处理联动
  205. * 因为要实现下拉头部放大的问题,tableView设置了contentInset,所以试图刚加载的时候会调用一遍这个方法,所以要做一些特殊处理,
  206. */
  207. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  208. if (scrollView == self.mainTableView) {
  209. //当前y轴偏移量
  210. CGFloat yOffset = scrollView.contentOffset.y;
  211. //临界点偏移量(吸顶临界点)
  212. CGFloat tabyOffset = [self.mainTableView rectForSection:0].origin.y - STATUS_BAR_HEIGHT - NAVIGATION_BAR_HEIGHT;
  213. //第一部分: 更改导航栏的背景图的透明度
  214. CGFloat alpha = 0;
  215. if (-yOffset <= STATUS_BAR_HEIGHT + NAVIGATION_BAR_HEIGHT) {
  216. alpha = 1;
  217. } else if ((-yOffset > STATUS_BAR_HEIGHT + NAVIGATION_BAR_HEIGHT) && -yOffset < self.HeaderImageViewHeight) {
  218. alpha = (self.HeaderImageViewHeight + yOffset) / (self.HeaderImageViewHeight - STATUS_BAR_HEIGHT - NAVIGATION_BAR_HEIGHT);
  219. }else {
  220. alpha = 0;
  221. }
  222. //第二部分:
  223. //利用contentOffset处理内外层scrollView的滑动冲突问题
  224. if (yOffset >= tabyOffset) {
  225. scrollView.contentOffset = CGPointMake(0, tabyOffset);
  226. _isTopIsCanNotMoveTabView = YES;
  227. }else{
  228. _isTopIsCanNotMoveTabView = NO;
  229. }
  230. _isTopIsCanNotMoveTabViewPre = !_isTopIsCanNotMoveTabView;
  231. if (!_isTopIsCanNotMoveTabViewPre) {
  232. [[NSNotificationCenter defaultCenter] postNotificationName:@"goTop" object:nil userInfo:@{@"canScroll":@"1"}];
  233. _canScroll = NO;
  234. } else{
  235. if (!_canScroll) {
  236. _mainTableView.contentOffset = CGPointMake(0, tabyOffset);
  237. }
  238. }
  239. //第三部分:
  240. /**
  241. * 处理头部自定义背景视图 (如: 下拉放大)
  242. * 图片会被拉伸多出状态栏的高度
  243. */
  244. if(yOffset <= -self.HeaderImageViewHeight) {
  245. if (_isEnlarge) {
  246. CGRect f = self.headerImageView.frame;
  247. //改变HeadImageView的frame
  248. //上下放大
  249. f.origin.y = yOffset;
  250. f.size.height = -yOffset;
  251. //左右放大
  252. f.origin.x = (yOffset * SCREEN_WIDTH / self.HeaderImageViewHeight + SCREEN_WIDTH) / 2;
  253. f.size.width = -yOffset * SCREEN_WIDTH / self.HeaderImageViewHeight;
  254. //改变头部视图的frame
  255. self.headerImageView.frame = f;
  256. }else{
  257. scrollView.bounces = NO;
  258. }
  259. }else {
  260. scrollView.bounces = YES;
  261. }
  262. }
  263. }
  264. #pragma mark - UITableViewDelegate
  265. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  266. return SCREEN_HEIGHT - STATUS_BAR_HEIGHT - NAVIGATION_BAR_HEIGHT;
  267. }
  268. #pragma mark - UITableViewDataSource
  269. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  270. return 1;
  271. }
  272. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  273. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  274. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  275. [cell.contentView addSubview:self.setPageViewControllers];
  276. return cell;
  277. }
  278. - (UITableView *)mainTableView {
  279. if (!_mainTableView) {
  280. //⚠️这里的属性初始化一定要放在mainTableView.contentInset的设置滚动之前, 不然首次进来视图就会偏移到临界位置,contentInset会调用scrollViewDidScroll这个方法。
  281. //初始化变量
  282. self.canScroll = YES;
  283. self.isTopIsCanNotMoveTabView = NO;
  284. self.mainTableView = [[CenterTouchTableView alloc]initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT) style:UITableViewStylePlain];
  285. _mainTableView.delegate = self;
  286. _mainTableView.dataSource = self;
  287. _mainTableView.showsVerticalScrollIndicator = NO;
  288. //注意:这里不能使用动态高度_headimageHeight, 不然tableView会往下移,在iphone X下,头部不放大的时候,上方依然会有白色空白
  289. _mainTableView.contentInset = UIEdgeInsetsMake(self.HeaderImageViewHeight, 0, 0, 0);//内容视图开始正常显示的坐标为(0, self.HeaderImageViewHeight)
  290. }
  291. return _mainTableView;
  292. }
  293. - (HomeSchoolHeadView *)headerContentView {
  294. if (!_headerContentView) {
  295. _headerContentView = [[HomeSchoolHeadView alloc]init];
  296. }
  297. return _headerContentView;
  298. }
  299. - (UIImageView *)headerImageView {
  300. if (!_headerImageView) {
  301. _headerImageView = [[UIImageView alloc] init];
  302. _headerImageView.backgroundColor = [UIColor clearColor];
  303. _headerImageView.userInteractionEnabled = YES;
  304. _headerImageView.frame = CGRectMake(0, -self.HeaderImageViewHeight, SCREEN_WIDTH, self.HeaderImageViewHeight);
  305. }
  306. return _headerImageView;
  307. }
  308. /*
  309. * 这里可以设置替换你喜欢的segmentView
  310. */
  311. - (UIView *)setPageViewControllers {
  312. if (!_segmentView) {
  313. //设置子控制器
  314. self.firstVC = [[FirstViewController alloc] init];
  315. self.secondVC = [[SecondViewController alloc] init];
  316. self.thirdVC = [[ThirdViewController alloc] init];
  317. NSArray *controllers = @[self.firstVC, self.secondVC, self.thirdVC];
  318. NSArray *titleArray = @[@"概况", @"人物", @"共享资源"];
  319. 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];
  320. //注意:不能通过初始化方法传递selectedIndex的初始值,因为内部使用的是Masonry布局的方式, 否则设置selectedIndex不起作用
  321. segmentView.selectedIndex = self.selectedIndex;
  322. _segmentView = segmentView;
  323. }
  324. return _segmentView;
  325. }
  326. - (UIView *)naviView {
  327. if (!_naviView) {
  328. _naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, STATUS_BAR_HEIGHT + NAVIGATION_BAR_HEIGHT)];
  329. _naviView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
  330. }
  331. return _naviView;
  332. }
  333. - (UIButton *)backBtn
  334. {
  335. if (!_backBtn) {
  336. _backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  337. [_backBtn setImage:[UIImage imageNamed:@"back_white_icon"] forState:(UIControlStateNormal)];
  338. _backBtn.frame = CGRectMake(0,STATUS_BAR_HEIGHT, 44, 44);
  339. _backBtn.adjustsImageWhenHighlighted = YES;
  340. [_backBtn addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
  341. }
  342. return _backBtn;
  343. }
  344. - (UIButton *)menuBtn
  345. {
  346. if (!_menuBtn) {
  347. _menuBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  348. [_menuBtn setImage:[UIImage imageNamed:@"menu_white_icon"] forState:(UIControlStateNormal)];
  349. _menuBtn.adjustsImageWhenHighlighted = YES;
  350. [_menuBtn addTarget:self action:@selector(menuAction) forControlEvents:(UIControlEventTouchUpInside)];
  351. }
  352. return _menuBtn;
  353. }
  354. - (UIButton *)shareBtn
  355. {
  356. if (!_shareBtn) {
  357. _shareBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  358. [_shareBtn setImage:[UIImage imageNamed:@"shareWhile"] forState:(UIControlStateNormal)];
  359. _shareBtn.adjustsImageWhenHighlighted = YES;
  360. [_shareBtn addTarget:self action:@selector(shareAction) forControlEvents:(UIControlEventTouchUpInside)];
  361. }
  362. return _shareBtn;
  363. }
  364. - (void)backAction
  365. {
  366. [self.navigationController popViewControllerAnimated:YES];
  367. }
  368. - (void)menuAction
  369. {
  370. // [super menuAction:nil];
  371. }
  372. - (void)shareAction
  373. {
  374. }
  375. @end