// // SegmentedPage.m // BaseProject // // Created by WindShan on 2016/12/9. // Copyright © 2016年 WindShan. All rights reserved. // #import "FileListPage.h" #import "HMSegmentedControl.h" #import "NetworkTestPage.h" #import "FileStatusView.h" #import "BaseNaviController.h" #import "FileDownView.h" #import "LHDB.h" @interface FileListPage () { NSInteger currentPage; } @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) HMSegmentedControl *segmentedControl; /*! views */ @property (nonatomic, strong) FileDownView * views1; // 已下 载 @property (nonatomic, strong) FileStatusView * views2; // 未下载 @end @implementation FileListPage #pragma mark - #pragma mark View Controller - (void)setupDocumentControllerWithURL:(NSURL *)url { if (self.docInteractionController == nil) { self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url]; self.docInteractionController.delegate = self; } else { self.docInteractionController.URL = url; } } #pragma mark - #pragma mark File system support - (NSString *)applicationDocumentsDirectory { return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; } - (void)directoryDidChange:(DirectoryWatcher *)folderWatcher { } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES; } #pragma mark - #pragma mark UIDocumentInteractionControllerDelegate - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController { return self; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBarHidden = NO; self.navigationItem.title = @"我的文件"; [self setNavigationLeft:@"返回" sel:@selector(backAction)]; /*! 设置CGRectZero从导航栏下开始计算 */ if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { self.edgesForExtendedLayout = UIRectEdgeNone; } self.view.backgroundColor = [UIColor whiteColor]; [self buildSegment]; // Do any additional setup after loading the view. } -(void)backAction { [self dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - ***** 配置segment - (void)buildSegment { self.segmentedControl.hidden = NO; self.scrollView.hidden = NO; self.views1.hidden = NO; self.views2.hidden = NO; } - (HMSegmentedControl *)segmentedControl { if (!_segmentedControl) { _segmentedControl = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)]; /*! 设置标题 */ _segmentedControl.sectionTitles = @[@"已下载",@"未下载"]; /*! 自适应宽度,随着屏幕滑动自动滚动 */ _segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; /*! 默认选中第0个view */ _segmentedControl.selectedSegmentIndex = 0; /*! 标题背景颜色 */ _segmentedControl.backgroundColor = [UIColor clearColor]; /*! 标题默认字体颜色 */ _segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : RgbColor(25, 31, 35), NSFontAttributeName: BA_FontSize(16)}; /*! 标题选中字体颜色 */ _segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : RgbColor(23, 172, 67), NSFontAttributeName: BA_FontSize(18)}; /*! 标题选中的下划线的颜色 */ _segmentedControl.selectionIndicatorColor = RgbColor(23, 172, 67); /*! 标题选中的下划线的高度 */ _segmentedControl.selectionIndicatorHeight = 1.0f; /*! 标题选中的样式:本样式为下划线 */ _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe; /*! 标题选中的下划线的方向:本样式为向下 */ _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown; /*! 标题的中间的隔线:默认为:NO */ _segmentedControl.verticalDividerEnabled = YES; /*! 标题的中间的隔线颜色 */ _segmentedControl.verticalDividerColor = RgbColor(23, 172, 67); /*! 标题的中间的隔线宽度 */ _segmentedControl.verticalDividerWidth = 1.0f; [self.view addSubview:_segmentedControl]; /*! 标题点击事件 */ BA_WEAKSELF; [_segmentedControl setIndexChangeBlock:^(NSInteger index) { if(index == 0 ) { [weakSelf.views1 reSetView]; } else if( index == 1 ) { [weakSelf.views2 reSetView]; } [weakSelf.scrollView scrollRectToVisible:CGRectMake(SCREEN_WIDTH * index, 0, SCREEN_WIDTH, weakSelf.scrollView.frame.size.height) animated:YES]; }]; //self.navigationItem.titleView = _segmentedControl; } return _segmentedControl; } - (UIScrollView *)scrollView { if (!_scrollView) { /*! 这里的frame按实际情况更改! */ self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, SCREEN_HEIGHT-72-64)]; self.scrollView.backgroundColor = [UIColor whiteColor]; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.bounces = NO; self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, self.scrollView.frame.size.height); self.scrollView.delegate = self; self.scrollView.backgroundColor = [UIColor whiteColor]; [self.scrollView scrollRectToVisible:CGRectMake(0, 0, SCREEN_WIDTH, self.scrollView.frame.size.height) animated:NO]; [self.view addSubview:self.scrollView]; } return _scrollView; } #pragma mark - ***** UIScrollViewDelegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pageWidth = scrollView.frame.size.width; currentPage = scrollView.contentOffset.x / pageWidth; LOG_INFO(@"current page:%ld",currentPage); if(currentPage == 0 ) { [self.views1 reSetView]; } else if( currentPage == 1 ) { [self.views2 reSetView]; } [_segmentedControl setSelectedSegmentIndex:currentPage animated:YES]; } - (FileDownView *)views1 { if (!_views1) { // start monitoring the document directory… self.docWatcher = [DirectoryWatcher watchFolderWithPath:[self applicationDocumentsDirectory] delegate:self]; _views1 = [[FileDownView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.scrollView.frame.size.height)]; _views1.delegate = self; [self.scrollView addSubview:_views1]; } return _views1; } - (void)didSelectedDownSection:(FileStatus*)model { NSURL *fileURL = [NSURL fileURLWithPath:model.filePath]; [self setupDocumentControllerWithURL:fileURL]; CGRect navRect = self.navigationController.navigationBar.frame; navRect.size = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT); [self.docInteractionController presentOptionsMenuFromRect:navRect inView:self.view animated:YES]; } - (FileStatusView *)views2 { if (!_views2) { _views2 = [[FileStatusView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, self.scrollView.frame.size.height)]; _views2.delegate = self; _views2.detailDelegate = self; [self.scrollView addSubview:_views2]; } return _views2; } - (void)didSelectedSection:(FileStatus*)model; { //DeviceDetailPage* page = [[DeviceDetailPage alloc] initIsFirstPage:NO]; //page.model = model; // 跳转界面 // BaseNaviController *baseNav = [[BaseNaviController alloc] initWithRootViewController:page]; // [self presentViewController:baseNav animated:YES completion:nil]; } - (void)didDetailSection:(FileStatus*)model; { [LHDBPath instanceManagerWith:DEFAULT_PATH]; [_views1 insertDataWithModel:model]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end