| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- //
- // OpenFileViewController.m
- // MusicGradeExam
- //
- // Created by Kyle on 2020/7/21.
- // Copyright © 2020 DayaMusic. All rights reserved.
- //
- #import "OpenFileViewController.h"
- #import <QuickLook/QuickLook.h>
- @interface OpenFileViewController ()<QLPreviewControllerDataSource, QLPreviewControllerDelegate>
- @property (nonatomic, strong) QLPreviewController *previewCtrl;
- @property (nonatomic, strong) NSMutableArray *fileArray;
- @end
- @implementation OpenFileViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self allocTitle:[NSString returnNoNullStringWithString:self.fileName]];
- [self addChildViewController:self.previewCtrl];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self requestFileSource];
- [self.previewCtrl addObserver:self forKeyPath:@"currentPreviewItemIndex" options:NSKeyValueObservingOptionNew context:nil];
- }
- - (void)requestFileSource { // 如果缓存中有,不会再次下载
-
- [MBProgressHUD ksShowHUDWithText:@"数据加载中......"];
- NSArray *urlArray = [self.urlString containsString:@","] ? [self.urlString componentsSeparatedByString:@","] : @[self.urlString];
- [KSRequestManager mutiDownloadFileRequest:urlArray progress:^(int64_t bytesRead, int64_t totalBytes) {
-
- } success:^(NSArray * _Nonnull dics) {
- [MBProgressHUD ksHideHUD];
- self.fileArray = [NSMutableArray arrayWithArray:dics];
- [self.view addSubview:self.previewCtrl.view];
- [self.previewCtrl refreshCurrentPreviewItem];
- } faliure:^(NSError * _Nonnull error) {
- [MBProgressHUD ksHideHUD];
- }];
- }
- #pragma mark ------ QLPreviewControllerDataSource
- - (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
- return self.fileArray[index];
- }
- - (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id<QLPreviewItem>)item {
- return NO;
- }
- - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController {
- return self.fileArray.count;
- }
- - (QLPreviewController *)previewCtrl {
- if (!_previewCtrl) {
- _previewCtrl = [[QLPreviewController alloc] init];
- _previewCtrl.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin);
- _previewCtrl.delegate = self;
- _previewCtrl.dataSource = self;
- _previewCtrl.currentPreviewItemIndex = 0;
- _previewCtrl.navigationController.navigationBar.userInteractionEnabled = NO;
- }
- return _previewCtrl;
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
- NSInteger currentIndex = [[change objectForKey:@"new"] integerValue];
- self.navigationController.interactivePopGestureRecognizer.enabled = (currentIndex == 0);
- [self rightButtonTitle:[NSString stringWithFormat:@"%zd/%zd", (currentIndex+1),self.fileArray.count]];
- }
- - (void)dealloc {
- [self.previewCtrl removeObserver:self forKeyPath:@"currentPreviewItemIndex"];
- }
- /*
- #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
|