当前位置:   article > 正文

OC IOS 文件解压缩预览_oc原生 解压文件

oc原生 解压文件

热很。。热很。。。。夏天的城市只有热浪没有情怀。。。

来吧,come on。。。

 引用第三方库:

pod 'SSZipArchive'

开发实现:

一、控制器实现

头文件控制器定义:

  1. //
  2. // ZipRarViewController.h
  3. //
  4. // Created by carbonzhao on 2024/5/28.
  5. //
  6. #import "IMBaseClassViewController.h"
  7. NS_ASSUME_NONNULL_BEGIN
  8. @interface ZipRarViewController : IMBaseClassViewController
  9. //可为本地路径,亦可为远端路径
  10. @property (nonatomic,strong) NSURL *filePath;
  11. //路径标记,相同标记解压过的不再解压
  12. @property (nonatomic,strong) NSString *fileFlag;
  13. @end
  14. NS_ASSUME_NONNULL_END

实现文件定义:

  1. //
  2. // ZipRarViewController.m
  3. //
  4. // Created by carbonzhao on 2024/5/28.
  5. //
  6. #import "ZipRarViewController.h"
  7. #import "SSZipArchive.h"
  8. #import "UIBreadView.h"
  9. NS_ASSUME_NONNULL_BEGIN
  10. @interface UINextTreeView : UIView
  11. {
  12. }
  13. - (void)setDataSource:(NSMutableArray*)list checkActionBlock:(void (^)(NSDictionary *e))checkActionBlock;
  14. @end
  15. NS_ASSUME_NONNULL_END
  16. typedef void (^SubTreeViewCheckedTreeBlock)(NSDictionary *e);
  17. @interface UINextTreeView ()<UITableViewDelegate,UITableViewDataSource>
  18. {
  19. NSMutableArray *dataList;
  20. UITableView *tview;
  21. }
  22. @property (nonatomic,copy) SubTreeViewCheckedTreeBlock ClickTreeBlock;
  23. @end
  24. @implementation UINextTreeView
  25. - (instancetype)initWithFrame:(CGRect)frame
  26. {
  27. if (self = [super initWithFrame:frame])
  28. {
  29. [self setupUI];
  30. }
  31. return self;
  32. }
  33. - (void)setFrame:(CGRect)frame
  34. {
  35. [super setFrame:frame];
  36. if (tview)
  37. {
  38. [tview setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  39. }
  40. }
  41. - (void)setupUI
  42. {
  43. dataList = [[NSMutableArray alloc] initWithCapacity:0];
  44. tview = [[UITableView alloc] initWithFrame:self.bounds];
  45. tview.backgroundColor = DSTextPlaceColor;
  46. tview.dataSource = self;
  47. tview.delegate = self;
  48. // tview.tableFooterView = [UIView new];
  49. tview.separatorColor = [UIColor clearColor];
  50. tview.estimatedRowHeight = 0;
  51. tview.estimatedSectionFooterHeight = 0;
  52. tview.estimatedSectionHeaderHeight = 0;
  53. [tview setShowsVerticalScrollIndicator:NO];
  54. [tview setShowsHorizontalScrollIndicator:NO];
  55. // [tview setBackgroundColor:[UIColor redColor]];
  56. // tview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  57. // [weakSelf getListAllDing:@"refresh"];
  58. // }];
  59. [self addSubview:tview];
  60. }
  61. #pragma mark - action
  62. - (void)setDataSource:(NSMutableArray*)list checkActionBlock:(void (^)(NSDictionary *e))checkActionBlock
  63. {
  64. [self setClickTreeBlock:checkActionBlock];
  65. [dataList addObjectsFromArray:list];
  66. [tview reloadData];
  67. }
  68. - (void)reloadView
  69. {
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. [self->tview reloadData];
  72. });
  73. }
  74. - (NSString *)docIcon:(NSString *)ext
  75. {
  76. NSString *ex = [ext lowercaseString];
  77. if ([@[@"xls",@"xlsx"] containsObject:ex])
  78. {
  79. return @"icon_excel_new";
  80. }
  81. else if ([@[@"doc",@"docx"] containsObject:ex])
  82. {
  83. return @"icon_word_new";
  84. }
  85. else if ([@[@"pdf"] containsObject:ex])
  86. {
  87. return @"icon_pdf_new";
  88. }
  89. else if ([@[@"txt"] containsObject:ex])
  90. {
  91. return @"icon_txt_new";
  92. }
  93. else if ([@[@"png",@"jpg",@"jpeg"] containsObject:ex])
  94. {
  95. return @"icon_img_new";
  96. }
  97. else if ([@[@"mov",@"mp4",@"avi",@"mpeg",@"mkv",@"3gp",@"wmv",@"rmvb"] containsObject:ex])
  98. {
  99. return @"icon_mp4_new";
  100. }
  101. else if ([@[@"mp3",@"wav"] containsObject:ex])
  102. {
  103. return @"icon_mp3";
  104. }
  105. else if ([@[@"zip",@"rar"] containsObject:ex])
  106. {
  107. return @"icon_zip_new";
  108. }
  109. return @"";
  110. }
  111. #pragma mark - tableView
  112. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  113. {
  114. return 1;
  115. }
  116. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  117. {
  118. return dataList.count;
  119. }
  120. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. CGFloat height = 58;
  123. return height;
  124. }
  125. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  128. CGFloat height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
  129. if (height > 0)
  130. {
  131. CGFloat x = 20;
  132. NSMutableDictionary *e = [dataList objectAtIndex:indexPath.row];
  133. kWeakSelf(self)
  134. NSString *dicon = [e boolForKey:@"isDir"]?@"icon_folder_icon":[self docIcon:[e stringForKey:@"ext"]];
  135. CGRect rt = CGRectMake(x, (height-30)/2, 30, 30);
  136. UIImageView *dirIcon = [[UIImageView alloc] initWithFrame:rt];
  137. [dirIcon setImage:IMAGE_INIM_NAME(dicon)];
  138. [cell.contentView addSubview:dirIcon];
  139. x += (rt.size.width + 10);
  140. NSString *name = [e stringForKey:@"fileName"];
  141. rt = CGRectMake(x, 0, tableView.frame.size.width-20-x-40, height);
  142. UILabel *label = [[UILabel alloc] initWithFrame:rt];
  143. [label setFont:Font_size(16)];
  144. [label setTextColor:RGBA(10, 10, 10, 1)];
  145. [label setText:name];
  146. [label setTextAlignment:NSTextAlignmentLeft];
  147. [cell.contentView addSubview:label];
  148. if ([e boolForKey:@"isDir"])
  149. {
  150. rt = CGRectMake(tableView.width-34, (height-14)/2, 14, 14);
  151. UIImageView *iconView = [[UIImageView alloc] initWithFrame:rt];
  152. [iconView setImage:IMAGE_INIM_NAME(@"icon_arrow")];
  153. [cell.contentView addSubview:iconView];
  154. }
  155. rt = CGRectMake(25, height-1, tableView.frame.size.width-25, 1);
  156. UIImageView *iconView = [[UIImageView alloc] initWithFrame:rt];
  157. [iconView setBackgroundColor:RGB(245, 245, 245)];
  158. [cell.contentView addSubview:iconView];
  159. rt = CGRectMake(0, 0, tableView.frame.size.width, height);
  160. iconView = [[UIImageView alloc] initWithFrame:rt];
  161. [iconView setBackgroundColor:RGB(245, 245, 245)];
  162. [cell setSelectedBackgroundView:iconView];
  163. }
  164. [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  165. return cell;
  166. }
  167. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  168. {
  169. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  170. NSMutableDictionary *e = [dataList objectAtIndex:indexPath.row];
  171. self.ClickTreeBlock(e);
  172. }
  173. @end
  174. @interface ZipRarViewController ()
  175. @property (nonatomic,strong) UIBreadView *breview;
  176. @end
  177. @implementation ZipRarViewController
  178. - (instancetype)init
  179. {
  180. if (self = [super init])
  181. {
  182. }
  183. return self;
  184. }
  185. - (void)viewDidLoad {
  186. [super viewDidLoad];
  187. // Do any additional setup after loading the view.
  188. self.breview = [UIBreadView breadViewWithFrame:CGRectMake(0, 0, ScreenWidth, self.safeAreaHeight) viewType:UIBreadViewBreadType];
  189. [self.view addSubview:self.breview];
  190. NSString *fileName = [self.filePath lastPathComponent]; //获取文件名称
  191. [self setTitle:[NSString stringWithFormat:@"%@预览",fileName]];
  192. if ([self.filePath.absoluteString hasPrefix:@"http"])
  193. {
  194. WeakSelf(self);
  195. [self showToastWithKeepAliveBlock:^(UIToastFenceView *waitView) {
  196. [waitView setViewMode:UIToastFenceViewModeCircle];
  197. [waitView setTitleText:@"下载中..."];
  198. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  199. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  200. NSURLRequest *request = [NSURLRequest requestWithURL:self.filePath];
  201. NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){
  202. CGFloat completedUnitCount = downloadProgress.completedUnitCount*1.0;
  203. CGFloat totalUnitCount = downloadProgress.totalUnitCount*1.0;
  204. CGFloat percent = completedUnitCount/totalUnitCount;
  205. [waitView setProgress:percent];
  206. if (percent == 1.0)
  207. {
  208. [waitView setViewMode:UIToastFenceViewModeText];
  209. [waitView setTitleText:@"下载完成"];
  210. [waitView dismiss:1.5];
  211. }
  212. } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
  213. NSURL *url = [NSURL fileURLWithPath:userDocuments(@"docPathFile", fileName)];
  214. return url;
  215. } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
  216. if (error) {
  217. [waitView setViewMode:UIToastFenceViewModeText];
  218. [waitView setTitleText:@"下载失败"];
  219. [waitView dismiss:1.5];
  220. }
  221. else
  222. {
  223. [waitView dismiss];
  224. NSURL *url = [NSURL fileURLWithPath:userDocuments(@"docPathFile", fileName)];
  225. weakSelf.filePath = url;
  226. [weakSelf toReadySource];
  227. }
  228. }];
  229. [downloadTask resume];
  230. }];
  231. }
  232. else
  233. {
  234. [self toReadySource];
  235. }
  236. }
  237. #pragma mark - 解压文件
  238. - (void)toReadySource
  239. {
  240. NSString *path = [self.filePath path];
  241. NSString *fileName = [[[self.filePath lastPathComponent] componentsSeparatedByString:@"."] firstObject];
  242. NSString *to = userDocuments(@"zipsPathFile", @"");
  243. to = [to stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@",self.fileFlag,fileName]];
  244. WeakSelf(self);
  245. // [[NSFileManager defaultManager] removeItemAtPath:to error:nil];
  246. if ([[NSFileManager defaultManager] fileExistsAtPath:to])
  247. {
  248. [self showToastBlock:^(UIToastFenceView *waitView) {
  249. [weakSelf readFiles:[to stringByAppendingPathComponent:fileName] fileName:fileName];
  250. }];
  251. }
  252. else
  253. {
  254. [self showToastBlock:^(UIToastFenceView *waitView)
  255. {
  256. [waitView setTitleText:@"解压中..."];
  257. BOOL flag = [SSZipArchive unzipFileAtPath:path toDestination:to];
  258. if (flag)
  259. {
  260. [weakSelf readFiles:[to stringByAppendingPathComponent:fileName] fileName:fileName];
  261. }
  262. else
  263. {
  264. [waitView setTitleText:@"解压失败"];
  265. [waitView waitForSeconds];
  266. }
  267. }];
  268. }
  269. }
  270. - (void)readFiles:(NSString *)path fileName:(NSString *)fileName
  271. {
  272. NSMutableArray *list = [NSMutableArray arrayWithCapacity:0];
  273. NSFileManager *fm = [NSFileManager defaultManager];
  274. NSArray *subFilePath = [fm contentsOfDirectoryAtPath:path error:nil];
  275. for (NSString * fileName in subFilePath)
  276. {
  277. if (![fileName isEqualToString:@".DS_Store"])
  278. {
  279. NSMutableDictionary *e = [[NSMutableDictionary alloc] initWithCapacity:0];
  280. [e setObject:path forKey:@"path"];
  281. [e setObject:fileName forKey:@"fileName"];
  282. BOOL isDir = NO;
  283. [fm fileExistsAtPath:[path stringByAppendingPathComponent:fileName] isDirectory: &isDir];
  284. [e setObject:boolToStr(isDir) forKey:@"isDir"];
  285. [e setObject:[fileName pathExtension] forKey:@"ext"];
  286. [list addObject:e];
  287. }
  288. }
  289. WeakSelf(self);
  290. dispatch_sync_on_main_queue(^{
  291. NSBreadData *d = [[NSBreadData alloc] init];
  292. [d setTitle:fileName];
  293. [self.breview addBreadViewTitle:d withConfigBlock:^UIView * _Nullable(NSInteger index, BOOL * _Nonnull useAnimation)
  294. {
  295. UINextTreeView *b = [[UINextTreeView alloc] init];
  296. [b setDataSource:list checkActionBlock:^(NSDictionary *e) {
  297. NSString *p = [e stringForKey:@"path"];
  298. NSString *n = [e stringForKey:@"fileName"];
  299. if ([e boolForKey:@"isDir"])
  300. {
  301. p = [p stringByAppendingPathComponent:n];
  302. [weakSelf readFiles:p fileName:n];
  303. }
  304. else
  305. {
  306. if (weakSelf.delegateBlock)
  307. {
  308. weakSelf.delegateBlock(e);
  309. }
  310. }
  311. }];
  312. return b;
  313. }];
  314. });
  315. }
  316. @end

二、面包屑实现(控制器内部实现切换子页面视图)

头文件源码:

  1. //
  2. // UIBreadView.h
  3. //
  4. // Copyright © 2021 dtx. All rights reserved.
  5. //
  6. #import <UIKit/UIKit.h>
  7. NS_ASSUME_NONNULL_BEGIN
  8. @interface NSBreadData : NSObject
  9. @property (nonatomic,strong) NSString *title;
  10. @end
  11. typedef NS_ENUM(NSInteger,UIBreadViewType){
  12. UIBreadViewBreadType=0, //面包屑模式,默认
  13. UIBreadViewSearchType, //搜索模式
  14. UIBreadViewHyborType, //混合模式(面包屑搜索均显示)
  15. };
  16. @interface UIBreadView : UIView
  17. //仅有一个面包屑及动画切换的容器
  18. + (UIBreadView *)breadViewWithFrame:(CGRect)frame viewType:(UIBreadViewType)viewType;
  19. //有一个面包屑及动画切换的容器及可添加的不进行动画切换的容器
  20. + (UIBreadView *)breadViewWithFrame:(CGRect)frame viewType:(UIBreadViewType)viewType positionFixView:(UIView * (^)(void))positionFixView;
  21. - (void)addBreadViewTitle:(NSBreadData *)data withConfigBlock:(UIView* _Nullable (^)(NSInteger index,BOOL *useAnimation)) setupBlock;
  22. //block返回YES,则隐藏面包屑
  23. - (void)registerSelectActionBlock:(BOOL (^)(NSInteger index))actionBlock;
  24. - (void)registerSearchKeywordBlock:(void (^)(NSString *keyword,BOOL willResign))aBlock;
  25. //搜索UI自定义
  26. - (void)registerConfigurationSearchInterfaceBlock:(void (^)(UIView *pview))aBlock;
  27. - (BOOL)canPopToOneView;
  28. - (CGFloat)breadNavigatorHeight;
  29. - (CGFloat)breadNavigatory;
  30. - (void)setHiddenNavigatorBreadHidden:(BOOL)flag;
  31. - (UIView *)parentView:(UIView *)selfView;
  32. - (UIView *)currentView;
  33. - (void)resignSearchFieldResponder;
  34. - (void)setReturnKeyType:(UIReturnKeyType)type;
  35. //面包屑清除数据回归初始状态
  36. - (void)breadcrumbClearingData;
  37. @end
  38. NS_ASSUME_NONNULL_END

实现文件:

  1. //
  2. // UIBreadView.m
  3. // Copyright © 2021 dtx. All rights reserved.
  4. //
  5. #import "UIBreadView.h"
  6. #import "NSExtentionSloter.h"
  7. #import "SearchBarDisplayCenter.h"
  8. #import "imHeaders.h"
  9. typedef BOOL (^UIBreadViewActionBlock)(NSInteger index);
  10. typedef void (^UIBreadViewSearchKeywordBlock)(NSString *keyword,BOOL willResign);
  11. @interface NSBreadData ()
  12. @property (nonatomic,assign) BOOL isSelected;
  13. @property (nonatomic,assign) CGFloat itemWidth;
  14. //如果有sourceView,则内部处理点击事件切换页面,如果无,将事件反馈到控制器处理
  15. @property (nonatomic,weak) UIView *sourceView;
  16. @end
  17. @implementation NSBreadData
  18. @synthesize title,sourceView,itemWidth,isSelected;
  19. @end
  20. @interface UIBreadView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,SearchBarDisplayCenterDelegate>
  21. {
  22. UICollectionView *textView;
  23. UIView *contentView;
  24. NSInteger lastHiglightedRow;
  25. NSMutableArray *dataList;
  26. }
  27. @property (nonatomic,copy) UIBreadViewActionBlock actionBlock;
  28. @property (nonatomic,copy) UIBreadViewSearchKeywordBlock searchBlock;
  29. //是否显示搜索组件进行搜索,如果显示了搜索则不再显示面包屑
  30. @property (nonatomic,assign) UIBreadViewType viewWorkType;
  31. @property (nonatomic,strong) UIView *fixedContainerView;
  32. @property (nonatomic, strong) UIImageView *sepView;
  33. @end
  34. @implementation UIBreadView
  35. @synthesize actionBlock,viewWorkType;
  36. + (UIBreadView *)breadViewWithFrame:(CGRect)frame viewType:(UIBreadViewType)viewType
  37. {
  38. UIBreadView *view = [[UIBreadView alloc] initWithFrame:frame];
  39. [view setViewWorkType:viewType];
  40. [view setupUI];
  41. return view;
  42. }
  43. + (UIBreadView *)breadViewWithFrame:(CGRect)frame viewType:(UIBreadViewType)viewType positionFixView:(UIView * (^)(void))positionFixView
  44. {
  45. UIBreadView *view = [[UIBreadView alloc] initWithFrame:frame];
  46. [view setViewWorkType:viewType];
  47. if (positionFixView)
  48. {
  49. UIView *fview = positionFixView();
  50. [view setFixedContainerView:fview];
  51. }
  52. [view setupUI];
  53. return view;
  54. }
  55. - (instancetype)initWithFrame:(CGRect)frame
  56. {
  57. if (self = [super initWithFrame:frame])
  58. {
  59. self->lastHiglightedRow = -1;
  60. self.viewWorkType = UIBreadViewBreadType;
  61. }
  62. return self;
  63. }
  64. - (void)setFrame:(CGRect)frame
  65. {
  66. [super setFrame:frame];
  67. CGRect rt = contentView.frame;
  68. rt.size.height = frame.size.height-rt.origin.y;
  69. [contentView setFrame:rt];
  70. for (UIView *aview in contentView.subviews)
  71. {
  72. [aview setFrame:CGRectMake(0, 0, frame.size.width, rt.size.height)];
  73. }
  74. }
  75. - (void)setupUI{
  76. dataList = [[NSMutableArray alloc] initWithCapacity:0];
  77. CGRect rt = CGRectMake(20, 0, self.bounds.size.width-40, 44);
  78. UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
  79. [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  80. textView = [[UICollectionView alloc] initWithFrame:rt collectionViewLayout:flowLayout];
  81. // textView.backgroundColor = [UIColor yellowColor];
  82. [textView setDelegate:self];
  83. [textView setDataSource:self];
  84. // [textView setPagingEnabled:YES];
  85. [textView setBackgroundColor:[UIColor clearColor]];
  86. [textView setShowsHorizontalScrollIndicator:NO];
  87. [textView setShowsVerticalScrollIndicator:NO];
  88. [textView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"dequeueIdentifier"];
  89. [self addSubview:textView];
  90. CGFloat y = rt.origin.y+rt.size.height;
  91. if (self.viewWorkType == UIBreadViewSearchType)
  92. {
  93. [textView setHidden:YES];
  94. CGRect rt = CGRectMake(0, 0, ScreenWidth, 50);
  95. UIView *header = [[UIView alloc]initWithFrame:rt];
  96. header.backgroundColor = [UIColor whiteColor];
  97. [header setTag:404];
  98. [self addSubview:header];
  99. SearchBarDisplayCenter *searchBar = [[SearchBarDisplayCenter alloc]initWithFrame:CGRectMake(0, 3, ScreenWidth, 47)];
  100. searchBar.userInteractionEnabled = YES;
  101. searchBar.placeholderStr = @"搜索";
  102. [searchBar breadChangeFrameAndSearchIcon];
  103. // searchBar.seachIconIV.image = [UIImage imageInImBundle:@"bread_search"];
  104. //
  105. // [searchBar.seachIconIV mas_updateConstraints:^(MASConstraintMaker *make) {
  106. //
  107. // make.width.mas_equalTo(17);
  108. // make.height.mas_equalTo(17);
  109. // }];
  110. //
  111. // searchBar.placeholderColor = colorFromText(@"#999999");
  112. [searchBar setTag:405];
  113. searchBar.backgroundColor = [UIColor whiteColor];
  114. [searchBar setDelegate:self];
  115. [header addSubview:searchBar];
  116. y = rt.origin.y+rt.size.height;
  117. }
  118. else if (self.viewWorkType == UIBreadViewHyborType)
  119. {
  120. rt = CGRectMake(0, 0, ScreenWidth, 50);
  121. UIView *header = [[UIView alloc]initWithFrame:rt];
  122. header.backgroundColor = [UIColor whiteColor];
  123. [header setTag:404];
  124. [self addSubview:header];
  125. SearchBarDisplayCenter *searchBar = [[SearchBarDisplayCenter alloc]initWithFrame:CGRectMake(0, 3, ScreenWidth, 47)];
  126. [searchBar setTag:405];
  127. searchBar.userInteractionEnabled = YES;
  128. searchBar.placeholderStr = @"搜索";
  129. [searchBar breadChangeFrameAndSearchIcon];
  130. [searchBar setDelegate:self];
  131. [header addSubview:searchBar];
  132. y = rt.origin.y+rt.size.height;
  133. rt = textView.frame;
  134. rt.origin.y = y;
  135. [textView setFrame:rt];
  136. y = rt.origin.y+rt.size.height;
  137. }
  138. [textView setDataObject:NSStringFromCGRect(textView.frame)];
  139. rt = CGRectMake(0, y, self.bounds.size.width, 10);
  140. UIImageView *sepView = [[UIImageView alloc] initWithFrame:rt];
  141. self.sepView = sepView;
  142. [sepView setBackgroundColor:DSTextPlaceColor];
  143. [self addSubview:sepView];
  144. y = rt.origin.y+rt.size.height;
  145. if (self.fixedContainerView)
  146. {
  147. rt = self.fixedContainerView.frame;
  148. rt.origin.y = y;
  149. [self.fixedContainerView setFrame:rt];
  150. [self addSubview:self.fixedContainerView];
  151. y = rt.origin.y+rt.size.height;
  152. }
  153. rt = CGRectMake(0, y, self.bounds.size.width, self.bounds.size.height- (rt.origin.y+rt.size.height));
  154. contentView = [[UIView alloc] initWithFrame:rt];
  155. [contentView setBackgroundColor:[UIColor whiteColor]];
  156. [contentView setTag:90001];
  157. [self addSubview:contentView];
  158. }
  159. - (void)addBreadViewTitle:(NSBreadData *)data withConfigBlock:(UIView* _Nullable (^)(NSInteger index,BOOL *useAnimation)) setupBlock
  160. {
  161. WeakSelf(self);
  162. __weak typeof(dataList) thisDataList = dataList;
  163. __weak typeof(textView) thisTextView = textView;
  164. __weak typeof(contentView) thisContentView = contentView;
  165. [textView performBatchUpdates:^{
  166. CGFloat w = [data.title stringSizeWithFont:[UIFont systemFontOfSize:14]].width;
  167. if (thisDataList.count > 0)
  168. {
  169. w += 28;
  170. }
  171. data.itemWidth = w;
  172. [data setIsSelected:NO];
  173. [thisDataList addObject:data];
  174. [thisTextView insertItemsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:thisDataList.count-1 inSection:0]]];
  175. if (thisDataList.count > 1)
  176. {
  177. NSBreadData *e = [thisDataList objectAtIndex:thisDataList.count-2];
  178. [e setIsSelected:YES];
  179. self->lastHiglightedRow = thisDataList.count-3;
  180. if (self->lastHiglightedRow > -1)
  181. {
  182. // e = [dataList objectAtIndex:lastHiglightedRow];
  183. // [e setIsSelected:NO];
  184. //
  185. // [textView reloadItemsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastHiglightedRow inSection:0]]];
  186. }
  187. [thisTextView reloadItemsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:thisDataList.count-2 inSection:0]]];
  188. }
  189. } completion:^(BOOL finished) {
  190. }];
  191. if (setupBlock)
  192. {
  193. NSInteger count = contentView.subviews.count;
  194. // for (NSInteger idx=0;idx<contentView.subviews.count;idx++)
  195. // {
  196. // UIView *view = [contentView.subviews objectAtIndex:idx];
  197. // CGRect rt = view.frame;
  198. // rt.origin.x = - (count-idx)*self.bounds.size.width;
  199. // [view setFrame:rt];
  200. // }
  201. BOOL useAnimation = YES;
  202. UIView *view = setupBlock(count,&useAnimation);
  203. __weak typeof(view) thisView = view;
  204. data.sourceView = view;
  205. if (view)
  206. {
  207. CGRect rt = view.frame;
  208. rt.origin.x = thisContentView.subviews.count>0?weakSelf.bounds.size.width:0;
  209. rt.size = thisContentView.frame.size;
  210. [view setFrame:rt];
  211. [thisContentView addSubview:thisView];
  212. if (thisContentView.subviews.count>0)
  213. {
  214. if (useAnimation)
  215. {
  216. [UIView animateWithDuration:0.3 animations:^
  217. {
  218. CGRect rt = thisView.frame;
  219. rt.origin.x = 0;
  220. [thisView setFrame:rt];
  221. }
  222. completion:^(BOOL finished)
  223. {
  224. }];
  225. }
  226. else
  227. {
  228. CGRect rt = thisView.frame;
  229. rt.origin.x = 0;
  230. [thisView setFrame:rt];
  231. }
  232. }
  233. }
  234. }
  235. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self->dataList.count-1 inSection:0];
  236. [self->textView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
  237. };
  238. - (void)setReturnKeyType:(UIReturnKeyType)type
  239. {
  240. SearchBarDisplayCenter *searchBar = [[self viewWithTag:404] viewWithTag:405];
  241. searchBar.searchField.returnKeyType = type;
  242. }
  243. - (UIView *)parentView:(UIView *)selfView
  244. {
  245. NSInteger index = [contentView.subviews indexOfObject:selfView];
  246. if (index > 0)
  247. {
  248. return [contentView.subviews objectAtIndex:index-1];
  249. }
  250. return nil;
  251. }
  252. - (UIView *)currentView
  253. {
  254. return [contentView.subviews lastObject];
  255. }
  256. - (void)registerSelectActionBlock:(BOOL (^)(NSInteger index))actionBlock
  257. {
  258. [self setActionBlock:actionBlock];
  259. }
  260. - (void)registerSearchKeywordBlock:(void (^)(NSString *keyword,BOOL willResign))aBlock
  261. {
  262. [self setSearchBlock:aBlock];
  263. }
  264. - (void)registerConfigurationSearchInterfaceBlock:(void (^)(UIView *pview))aBlock
  265. {
  266. SearchBarDisplayCenter *searchBar = [[self viewWithTag:404] viewWithTag:405];
  267. CGRect ft = searchBar.frame;
  268. UIView *aview = [[UIView alloc] initWithFrame:ft];
  269. [aview setUserInteractionEnabled:YES];
  270. [[self viewWithTag:404] addSubview:aview];
  271. aBlock(aview);
  272. }
  273. - (CGSize)itemSizeWithIndex:(NSIndexPath *)indexPath{
  274. NSBreadData *data = [dataList objectAtIndex:indexPath.row];
  275. CGFloat w = data.itemWidth;
  276. return CGSizeMake(w+5, textView.frame.size.height);
  277. }
  278. - (void)setHiddenNavigatorBreadHidden:(BOOL)flag
  279. {
  280. if (textView.isHidden != flag)
  281. {
  282. [textView setHidden:flag];
  283. if (flag)
  284. {
  285. [UIView animateWithDuration:0.3 animations:^
  286. {
  287. CGRect rt = self->textView.frame;
  288. rt.origin.y = -rt.size.height;
  289. [self->textView setFrame:rt];
  290. CGFloat y = 0;
  291. if ([self viewWithTag:404])
  292. {
  293. y = [self viewWithTag:404].frame.origin.y+[self viewWithTag:404].frame.size.height;
  294. }
  295. rt = CGRectMake(0, y, self.bounds.size.width, 5);
  296. [self.sepView setFrame:rt];
  297. y += 5;
  298. rt = CGRectMake(0, y, self.bounds.size.width, self.bounds.size.height-y);
  299. [self->contentView setFrame:rt];
  300. }
  301. completion:^(BOOL finished)
  302. {
  303. CGRect rt = self->textView.frame;
  304. rt.origin.x = self.bounds.size.width;
  305. rt.origin.y = 0;
  306. [self->textView setFrame:rt];
  307. }];
  308. }
  309. else
  310. {
  311. [UIView animateWithDuration:0.3 animations:^
  312. {
  313. CGRect rt = self->textView.frame;
  314. rt.origin.x = 20;
  315. rt.origin.y = CGRectFromString((NSString *)[self->textView dataObject]).origin.y;
  316. [self->textView setFrame:rt];
  317. CGFloat y = rt.origin.y+rt.size.height;
  318. rt = CGRectMake(0, y, self.bounds.size.width, 5);
  319. [self.sepView setFrame:rt];
  320. y += 5;
  321. rt = CGRectMake(0, y, self.bounds.size.width, self.bounds.size.height-y);
  322. [self->contentView setFrame:rt];
  323. }
  324. completion:^(BOOL finished)
  325. {
  326. }];
  327. }
  328. }
  329. for (UIView *aview in contentView.subviews)
  330. {
  331. [aview setFrame:CGRectMake(0, 0, contentView.size.width, contentView.size.height)];
  332. }
  333. }
  334. - (BOOL)canPopToOneView
  335. {
  336. BOOL flag = NO;
  337. if (dataList.count==0) {
  338. return flag;
  339. }
  340. NSBreadData *mObject = [dataList lastObject];
  341. if (mObject.sourceView && dataList.count>1)
  342. {
  343. flag = YES;
  344. [self poptoOneViewWithAnimation:dataList.count-2];
  345. }
  346. return flag;
  347. }
  348. - (CGFloat)breadNavigatorHeight
  349. {
  350. if (self.viewWorkType == UIBreadViewSearchType)
  351. {
  352. return MAX(textView.frame.size.height, [self viewWithTag:404].frame.size.height);
  353. }
  354. else if (self.viewWorkType == UIBreadViewHyborType)
  355. {
  356. return textView.frame.size.height+[self viewWithTag:404].frame.size.height;
  357. }
  358. else
  359. {
  360. return textView.frame.size.height;
  361. }
  362. }
  363. - (CGFloat)breadNavigatory
  364. {
  365. return [self viewWithTag:404].frame.origin.y+[self viewWithTag:404].frame.size.height;
  366. }
  367. - (void)resignSearchFieldResponder
  368. {
  369. SearchBarDisplayCenter *searchBar = [[self viewWithTag:404] viewWithTag:405];
  370. [searchBar restoreInitialState];
  371. [searchBar.searchField resignFirstResponder];
  372. [self poptoOneViewWithAnimation:1];
  373. }
  374. - (void)poptoOneViewWithAnimation:(NSInteger)row
  375. {
  376. if (self.actionBlock)
  377. {
  378. BOOL flag = self.actionBlock(row);
  379. [self setHiddenNavigatorBreadHidden:flag];
  380. }
  381. NSBreadData *mObject = [dataList objectAtIndex:row];
  382. if (mObject.sourceView)
  383. {
  384. for (NSInteger ix=dataList.count-1; ix>row;ix--)
  385. {
  386. NSBreadData *mObject = [dataList objectAtIndex:ix];
  387. UIView *aview = mObject.sourceView;
  388. [UIView animateWithDuration:0.3 animations:^
  389. {
  390. CGRect rt = aview.frame;
  391. rt.origin.x = self.bounds.size.width;
  392. [aview setFrame:rt];
  393. }
  394. completion:^(BOOL finished)
  395. {
  396. [aview removeFromSuperview];
  397. }];
  398. }
  399. [dataList removeObjectsInRange:NSMakeRange(row+1, dataList.count- (row+1))];
  400. NSBreadData *m = [dataList lastObject];
  401. [m setIsSelected:NO];
  402. if (dataList.count > 1)
  403. {
  404. m = [dataList objectAtIndex:dataList.count-2];
  405. [m setIsSelected:YES];
  406. }
  407. if (textView)
  408. {
  409. [textView reloadSections:[NSIndexSet indexSetWithIndex:0]];
  410. }
  411. }
  412. }
  413. - (void)breadcrumbClearingData {
  414. if (self.actionBlock)
  415. {
  416. BOOL flag = self.actionBlock(0);
  417. [self setHiddenNavigatorBreadHidden:flag];
  418. }
  419. if (dataList.count > 0) {
  420. // for (NSInteger ix=0; ix>dataList.count;ix++)
  421. // {
  422. // NSBreadData *mObject = [dataList objectAtIndex:ix];
  423. // UIView *aview = mObject.sourceView;
  424. // [aview removeFromSuperview];
  425. // }
  426. [dataList removeAllObjects];
  427. }
  428. if (textView)
  429. {
  430. [textView reloadSections:[NSIndexSet indexSetWithIndex:0]];
  431. }
  432. }
  433. #pragma mark - SearchBarDisplayCenterDelegate
  434. - (void)getSearchKeyWordWhenChanged:(NSString *)searchWord
  435. {
  436. if (self.searchBlock)
  437. {
  438. self.searchBlock(searchWord,NO);
  439. }
  440. }
  441. - (void)textFieldBecomeResponder:(NSString*)text
  442. {
  443. if (self.searchBlock)
  444. {
  445. self.searchBlock(text,NO);
  446. }
  447. }
  448. - (void)textFieldResignResponder:(NSString*)text
  449. {
  450. if (self.searchBlock)
  451. {
  452. self.searchBlock(text,YES);
  453. }
  454. }
  455. #pragma mark - UICollectionView
  456. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  457. {
  458. return 1;
  459. }
  460. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  461. {
  462. NSInteger count = dataList.count;
  463. return count;
  464. }
  465. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  466. {
  467. return 0;
  468. }
  469. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  470. {
  471. return 0;
  472. }
  473. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  474. {
  475. return [self itemSizeWithIndex:indexPath];
  476. }
  477. // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
  478. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  479. {
  480. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dequeueIdentifier" forIndexPath:indexPath];
  481. [cell.contentView setBackgroundColor:[UIColor clearColor]];
  482. CGSize size = [self itemSizeWithIndex:indexPath];
  483. NSBreadData *mObject = [dataList objectAtIndex:indexPath.row];
  484. CGFloat x = 5;
  485. if (indexPath.row>0)
  486. {
  487. CGRect rt = CGRectMake(x, (size.height-10)/2, 10, 10);
  488. UIImageView *iconView = [cell.contentView viewWithTag:90];
  489. if (!iconView)
  490. {
  491. iconView = [[UIImageView alloc] initWithFrame:rt];
  492. [iconView setImage:IMAGE_INIM_NAME(@"bookList_xiayiye")];
  493. [iconView setTag:90];
  494. [cell.contentView addSubview:iconView];
  495. }
  496. if (mObject.isSelected)
  497. {
  498. [iconView setTintColor:RGB(0, 125, 255)];
  499. }
  500. x += rt.size.width;
  501. }
  502. [[cell.contentView viewWithTag:90] setHidden:indexPath.row == 0];
  503. CGRect rt = CGRectMake(x, 0, size.width-x, size.height);
  504. UIButton *button = [cell.contentView viewWithTag:91];
  505. if (!button)
  506. {
  507. button = [UIButton buttonWithType:UIButtonTypeCustom];
  508. [button setTag:91];
  509. [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
  510. [button setTitleColor:RGB(40, 40, 40) forState:UIControlStateNormal];
  511. [button setTitleColor:RGB(0, 125, 255) forState:UIControlStateSelected];
  512. [cell.contentView addSubview:button];
  513. }
  514. [button setUserInteractionEnabled:NO];
  515. [button setFrame:rt];
  516. [button setTitle:mObject.title forState:UIControlStateNormal];
  517. [button setDataObject:mObject];
  518. [button setSelected:mObject.isSelected];
  519. [cell setBackgroundColor:[UIColor clearColor]];
  520. return cell;
  521. }
  522. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  523. {
  524. // UICollectionViewCell *cell = [contentView cellForItemAtIndexPath:indexPath];
  525. [self poptoOneViewWithAnimation:indexPath.row];
  526. }
  527. @end

完毕,相关的资源文件自己寻找整理,这里就不贴了,相关的一些个人开发依赖,有兴趣的同学可以留言找我要! 

三、使用

  1. ZipRarViewController *c = [[ZipRarViewController alloc] init];
  2. //传入压缩包路径,支持本地文件及远程文件URL
  3. [c setFilePath:url];
  4. //文中路径进行MD5,目的是区分记录解压记录,已解压过的后期不再解压而是直接读取展示
  5. [c setFileFlag:[fileFlag md5Byte32Lower]];
  6. [c setDelegateBlock:^(NSDictionary *data) {
  7. NSString *p = [data stringForKey:@"path"];
  8. NSString *f = [data stringForKey:@"fileName"];
  9. weakSelf.fileURL = [NSURL fileURLWithPath:[p
  10. stringByAppendingPathComponent:f]];
  11. [weakSelf openDocument:weakSelf.fileURL];
  12. }];
  13. [self.navigationController pushViewController:c animated:YES];
  14. //打开文件方法
  15. - (void)openDocument:(NSURL *)url {
  16. if ([QLPreviewController canPreviewItem:url]) {
  17. dispatch_async(dispatch_get_main_queue(), ^{
  18. [weakSelf initKkpreview];
  19. });
  20. }
  21. }
  22. - (void)initKkpreview {
  23. self.kkpreview = [[QLPreviewController alloc] init];
  24. self.kkpreview.delegate = self;
  25. self.kkpreview.dataSource = self;
  26. WeakSelf(self);
  27. UIView *rview = [UINaviButton naviButtonWithActionBlock:^{
  28. [weakSelf.kkpreview.navigationController popViewControllerAnimated:YES];
  29. }];
  30. UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:rview];
  31. self.kkpreview.navigationItem.leftBarButtonItem = item;
  32. [self.navigationController pushViewController:self.kkpreview animated:YES];
  33. }
  34. #pragma mark - QLPreviewControllerDataSource
  35. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
  36. return 1;
  37. }
  38. - (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
  39. return self.fileURL;
  40. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/677913
推荐阅读
相关标签
  

闽ICP备14008679号