// // TFComicDetailRightViewController.m // TFReader // // Created by 谢腾飞 on 2020/12/19. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFComicDetailRightViewController.h" #import "TFComicDetailRightViewCell.h" #import "TFComicBrowseViewController.h" #import "TFReadRecordManager.h" #define MenuButtonHeight 50 @interface TFComicDetailRightViewController () { TFButton *gotoCurrent; TFButton *gotoEnd; CGFloat beginOffSetY; BOOL clickGotoButton; } @property (nonatomic, strong) UILabel *headTitleLabel; @property (nonatomic, strong) TFButton *sortButton; /// 已阅读章节索引 @property (nonatomic, assign) NSInteger readIndex; @end @implementation TFComicDetailRightViewController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.mainTableView reloadData]; } - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; } - (void)initialize { // 计算已读章节的位置 NSInteger readIndex = [[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeComic] getReadingRecordChapter_idWithProduction_id:self.comicModel.production_id]; NSArray *t_arr = [self.comicModel.chapter_list valueForKeyPath:@"chapter_id"]; self.readIndex = [t_arr indexOfObject:@(readIndex)]; if (self.readIndex == NSNotFound) { self.readIndex = -1; } [self hiddenNavigationBar:YES]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadRecordData) name:NSNotification_Updata_Read_Record object:nil]; } - (void)createSubviews { self.mainTableView.delegate = self; self.mainTableView.dataSource = self; [self.view addSubview:self.mainTableView]; [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); }]; gotoCurrent = [[TFButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - MenuButtonHeight - kHalfMargin, SCREEN_HEIGHT - Comic_Detail_HeaderView_Height - PUB_TABBAR_HEIGHT - 44 - 44 - MenuButtonHeight - kMargin - kHalfMargin, MenuButtonHeight, MenuButtonHeight) buttonTitle:TFLocalizedString(@"当前") buttonImageName:@"comic_goto_current" buttonIndicator:TFButtonIndicatorTitleBottom]; gotoCurrent.layer.cornerRadius = 25; gotoCurrent.layer.borderWidth = 1; gotoCurrent.layer.borderColor = kGrayViewColor.CGColor; gotoCurrent.clipsToBounds = YES; gotoCurrent.backgroundColor = kWhiteColor; gotoCurrent.buttonMargin = 8; gotoCurrent.buttonTitleFont = kFont10; gotoCurrent.graphicDistance = 0; gotoCurrent.buttonImageScale = 0.4; [gotoCurrent addTarget:self action:@selector(gotoCurrentClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:gotoCurrent]; gotoEnd = [[TFButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - MenuButtonHeight - kHalfMargin, SCREEN_HEIGHT - Comic_Detail_HeaderView_Height - PUB_TABBAR_HEIGHT - 44 - 44 - kMargin, MenuButtonHeight, MenuButtonHeight) buttonTitle:TFLocalizedString(@"到底") buttonImageName:@"comic_goto_bottom" buttonIndicator:TFButtonIndicatorTitleBottom]; gotoEnd.layer.cornerRadius = 25; gotoEnd.layer.borderWidth = 1; gotoEnd.layer.borderColor = kGrayViewColor.CGColor; gotoEnd.clipsToBounds = YES; gotoEnd.tag = 0; gotoEnd.backgroundColor = kWhiteColor; gotoEnd.buttonMargin = 8; gotoEnd.buttonTitleFont = kFont10; gotoEnd.graphicDistance = 0; gotoEnd.buttonImageScale = 0.4; [gotoEnd addTarget:self action:@selector(gotoEndClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:gotoEnd]; [self setEmptyOnView:self.mainTableView title:TFLocalizedString(@"暂无目录列表") buttonTitle:@"" tapBlock:^{}]; if (self.comicModel.chapter_list.count == 0) { gotoCurrent.hidden = YES; gotoEnd.hidden = YES; self.sortButton.hidden = YES; self.headTitleLabel.hidden = YES; } else { gotoCurrent.hidden = NO; gotoEnd.hidden = NO; self.sortButton.hidden = NO; self.headTitleLabel.hidden = NO; } dispatch_async(dispatch_get_main_queue(), ^{ [self.mainTableView xtfei_endLoading]; }); } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { beginOffSetY = scrollView.contentOffset.y; clickGotoButton = NO; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (!clickGotoButton) { if ([scrollView isEqual:self.mainTableView]) { if (!self.canScroll) { scrollView.contentOffset = CGPointZero; } if (scrollView.contentOffset.y <= 0) { self.canScroll = NO; scrollView.contentOffset = CGPointZero; //到顶通知父视图改变状态 [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Can_Leave_Top object:@YES]; } else { [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Can_Leave_Top object:@NO]; } } if (scrollView.contentOffset.y > beginOffSetY) { gotoEnd.buttonImageName = @"comic_goto_bottom"; gotoEnd.buttonTitle = TFLocalizedString(@"到底"); gotoEnd.tag = 0; } else if (scrollView.contentOffset.y < beginOffSetY) { gotoEnd.buttonImageName = @"comic_goto_top"; gotoEnd.buttonTitle = TFLocalizedString(@"到顶"); gotoEnd.tag = 1; } } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView.contentSize.height - scrollView.contentOffset.y <= CGRectGetHeight(scrollView.bounds)) {// 滑到了底部 gotoEnd.buttonImageName = @"comic_goto_top"; gotoEnd.buttonTitle = TFLocalizedString(@"到顶"); gotoEnd.tag = 1; } else if (scrollView.contentOffset.y == 0) {// 滑到了顶部 gotoEnd.buttonImageName = @"comic_goto_bottom"; gotoEnd.buttonTitle = TFLocalizedString(@"到底"); gotoEnd.tag = 0; } } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { if (scrollView.contentSize.height - scrollView.contentOffset.y <= CGRectGetHeight(scrollView.bounds)) {// 滑到了底部 gotoEnd.buttonImageName = @"comic_goto_top"; gotoEnd.buttonTitle = TFLocalizedString(@"到顶"); gotoEnd.tag = 1; } else if (scrollView.contentOffset.y == 0) {// 滑到了顶部 gotoEnd.buttonImageName = @"comic_goto_bottom"; gotoEnd.buttonTitle = TFLocalizedString(@"到底"); gotoEnd.tag = 0; } } } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { if (scrollView.contentSize.height - scrollView.contentOffset.y <= CGRectGetHeight(scrollView.bounds)) {// 滑到了底部 gotoEnd.buttonImageName = @"comic_goto_top"; gotoEnd.buttonTitle = TFLocalizedString(@"到顶"); gotoEnd.tag = 1; } else if (scrollView.contentOffset.y == 0) {// 滑到了顶部 gotoEnd.buttonImageName = @"comic_goto_bottom"; gotoEnd.buttonTitle = TFLocalizedString(@"到底"); gotoEnd.tag = 0; } } - (void)setContentOffSetY:(CGFloat)contentOffSetY { gotoCurrent.frame = CGRectMake(SCREEN_WIDTH - MenuButtonHeight - kHalfMargin, SCREEN_HEIGHT - Comic_Detail_HeaderView_Height - PUB_TABBAR_HEIGHT - 44 - 44 - MenuButtonHeight - kMargin - kHalfMargin + contentOffSetY, MenuButtonHeight, MenuButtonHeight); gotoEnd.frame = CGRectMake(SCREEN_WIDTH - MenuButtonHeight - kHalfMargin, SCREEN_HEIGHT - Comic_Detail_HeaderView_Height - PUB_TABBAR_HEIGHT - 44 - 44 - kMargin + contentOffSetY, MenuButtonHeight, MenuButtonHeight); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.comicModel.chapter_list.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TFProductionChapterModel *t_chapterList = [self.comicModel.chapter_list objectOrNilAtIndex:indexPath.row]; TFComicDetailRightViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFComicDetailRightViewCell"]; if (!cell) { cell = [[TFComicDetailRightViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFComicDetailRightViewCell"]; } cell.chapterModel = t_chapterList; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)]; view.backgroundColor = [UIColor whiteColor]; [view addSubview:self.headTitleLabel]; [view addSubview:self.sortButton]; return view; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.readIndex = indexPath.row; TFProductionChapterModel *t_chapterModel = [self.comicModel.chapter_list objectOrNilAtIndex:indexPath.row]; [[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeComic] addReadingRecordWithProduction_id:t_chapterModel.production_id chapter_id:t_chapterModel.chapter_id chapterTitle:t_chapterModel.chapter_title]; TFComicBrowseViewController *vc = [[TFComicBrowseViewController alloc] init]; vc.comicProductionModel = self.comicModel; vc.chapter_id = t_chapterModel.chapter_id; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - 点击事件 - (void)changeDirectorySort:(TFButton *)sender { if (self.comicModel.chapter_list.count == 0) return; if (sender.tag == 0) { sender.tag = 1; sender.buttonImageName = @"comic_reverse_order"; sender.buttonTitle = TFLocalizedString(@"倒序"); } else { sender.tag = 0; sender.buttonImageName = @"comic_positive_order"; sender.buttonTitle = TFLocalizedString(@"正序"); } self.comicModel.chapter_list = [[self.comicModel.chapter_list reverseObjectEnumerator] allObjects]; // 计算已读章节的位置 NSInteger readIndex = [[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeComic] getReadingRecordChapter_idWithProduction_id:self.comicModel.production_id]; NSArray *t_arr = [self.comicModel.chapter_list valueForKeyPath:@"chapter_id"]; self.readIndex = [t_arr indexOfObject:@(readIndex)]; if (self.readIndex == NSNotFound) { self.readIndex = -1; } [self.mainTableView reloadData]; } - (void)gotoCurrentClick:(UIButton *)sender { if (self.comicModel.chapter_list.count == 0) return; [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Directory_Move object:@"bottom"]; if (self.readIndex != -1) { [self.mainTableView scrollToRow:self.readIndex inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES]; } } - (void)gotoEndClick:(UIButton *)sender { if (self.comicModel.chapter_list.count == 0) return; clickGotoButton = YES; [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Can_Leave_Top object:@NO]; if (sender.tag == 0) { [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Directory_Move object:@"bottom"]; [self.mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.comicModel.chapter_list.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; gotoEnd.buttonImageName = @"comic_goto_top"; gotoEnd.buttonTitle = TFLocalizedString(@"到顶"); gotoEnd.tag = 1; } else { [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Directory_Move object:@"top"]; [self.mainTableView setContentOffset:CGPointMake(0,0) animated:YES]; gotoEnd.buttonImageName = @"comic_goto_bottom"; gotoEnd.buttonTitle = TFLocalizedString(@"到底"); gotoEnd.tag = 0; } } #pragma mark - 懒加载 - (UILabel *)headTitleLabel { if (!_headTitleLabel) { _headTitleLabel = [[UILabel alloc] init]; _headTitleLabel.text = self.comicModel.flag?:@""; _headTitleLabel.frame = CGRectMake(kHalfMargin, 0, SCREEN_WIDTH - kMargin, 44); _headTitleLabel.font = kFont11; _headTitleLabel.textColor = kGrayTextColor; _headTitleLabel.textAlignment = NSTextAlignmentLeft; } return _headTitleLabel; } - (TFButton *)sortButton { if (!_sortButton) { CGFloat width = [TFViewHelper getDynamicWidthWithLabelFont:kFont11 labelHeight:30.0 labelText:TFLocalizedString(@"正序") maxWidth:SCREEN_WIDTH / 2.0 - kMargin]; width += kLabelHeight; _sortButton = [[TFButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - kHalfMargin - width, 7, width, 30) buttonTitle:TFLocalizedString(@"正序") buttonSubTitle:@"" buttonImageName:@"comic_positive_order" buttonIndicator:TFButtonIndicatorImageRightBothRight showMaskView:NO]; _sortButton.buttonImageScale = 0.4; _sortButton.buttonTitleFont = kFont11; _sortButton.graphicDistance = 5; _sortButton.buttonTitleColor = kGrayTextColor; _sortButton.tag = 0; [_sortButton addTarget:self action:@selector(changeDirectorySort:) forControlEvents:UIControlEventTouchUpInside]; } return _sortButton; } - (void)setComicModel:(TFProductionModel *)comicModel { _comicModel = comicModel; if (self.sortButton.tag == 1) { self.comicModel.chapter_list = [[comicModel.chapter_list reverseObjectEnumerator] allObjects]; } [self reloadRecordData]; } - (void)reloadRecordData { [self.mainTableView reloadData]; } @end