BaseViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //
  2. // BaseViewController.m
  3. // Private-X
  4. //
  5. // Created by 余衡武 on 2021/12/8.
  6. //
  7. #import "BaseViewController.h"
  8. #import "FLAnimatedImageView.h"
  9. #import "FLAnimatedImage.h"
  10. #import "HWWebViewController.h"
  11. #import "HWPageViewController.h"
  12. @interface BaseViewController () <HWToolBarDelegate,HWSearchBarDelegate>
  13. {
  14. UIView *wattingView;
  15. BOOL needSowWattingView;
  16. FLAnimatedImageView *wattingViewImageView;
  17. }
  18. @end
  19. @implementation BaseViewController
  20. @synthesize wattingViewForHaveStr;
  21. @synthesize needSowWattingViewStr;
  22. @synthesize navBarBGView;
  23. @synthesize backBtn;
  24. @synthesize titleLabel;
  25. #pragma mark 生命周期
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. _canBack = YES;
  29. [self drawBaseView];
  30. }
  31. - (void)viewWillAppear:(BOOL)animated
  32. {
  33. [super viewWillAppear:animated];
  34. NSArray*vcArr = self.navigationController.viewControllers;
  35. if(vcArr.count > 1){
  36. [self hideTabbarFun];
  37. }
  38. else{
  39. [self showTabbarFun];
  40. }
  41. }
  42. //- (void)viewDidAppear:(BOOL)animated {
  43. // [super viewDidAppear:animated];
  44. //
  45. // NSArray*vcArr = self.navigationController.viewControllers;
  46. // if(vcArr.count > 1){
  47. // [self hideTabbarFun];
  48. // }
  49. // else{
  50. // [self showTabbarFun];
  51. // }
  52. //}
  53. - (void)showTabbarFun
  54. {
  55. // 显示 tab bar
  56. self.tabBarController.tabBar.hidden = NO;
  57. }
  58. - (void)hideTabbarFun
  59. {
  60. // 隐藏 tab bar
  61. self.tabBarController.tabBar.hidden = YES;
  62. }
  63. //- (UIStatusBarStyle)preferredStatusBarStyle {
  64. // return UIStatusBarStyleDefault;
  65. //}
  66. #pragma mark UI布局
  67. - (void)drawBaseView {
  68. self.navigationController.navigationBar.hidden = YES;
  69. self.view.backgroundColor = HW1D1E1FColor;
  70. // 导航栏
  71. self.navigationBar = [HWSearchBar shareInstance];
  72. self.navigationBar.delegate = self;
  73. [self.navigationBar setBackgroundColor:HW1D1E1FColor];
  74. [self.view addSubview:self.navigationBar];
  75. [self.navigationBar mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.top.mas_equalTo(0);
  77. make.left.mas_equalTo(0);
  78. make.right.mas_equalTo(0);
  79. make.height.mas_equalTo(64.f+AdaptNaviHeight);
  80. }];
  81. //self.navigationBar.hidden = YES;
  82. // 底部工具条
  83. self.toolBar = [HWToolBar shareInstance];
  84. self.toolBar.delegate = self;
  85. [self.view addSubview:self.toolBar];
  86. [self.toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.bottom.mas_equalTo(0);
  88. make.left.mas_equalTo(0);
  89. make.width.mas_equalTo(SCREEN_W);
  90. make.height.mas_equalTo(TABBARHEIGHT);
  91. }];
  92. //self.toolBar.hidden = YES;
  93. navBarBGView = [[UIView alloc] init];
  94. [navBarBGView setBackgroundColor:HWF5F7FAColor];
  95. [self.view addSubview:navBarBGView];
  96. [navBarBGView mas_makeConstraints:^(MASConstraintMaker *make) {
  97. make.top.mas_equalTo(0);
  98. make.left.mas_equalTo(0);
  99. make.right.mas_equalTo(0);
  100. make.height.mas_equalTo(H_STATE_BAR + 64.f);
  101. }];
  102. [navBarBGView setHidden:YES];
  103. CGFloat btn_w_h = 40;
  104. CGFloat btn_show = 28;
  105. backBtn = [[UIButton alloc] init];
  106. [navBarBGView addSubview:backBtn];
  107. [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  108. make.top.mas_equalTo(H_STATE_BAR + (64.f - btn_w_h)/2.f);
  109. make.left.mas_equalTo(10);
  110. make.width.mas_equalTo(btn_w_h);
  111. make.height.mas_equalTo(btn_w_h);
  112. }];
  113. [backBtn setImage:[UIImage imageNamed:@"icon_base_back"] forState:(UIControlStateNormal)];
  114. [backBtn setImageEdgeInsets:(UIEdgeInsetsMake((btn_w_h - btn_show)/2.f, (btn_w_h - btn_show)/2.f, (btn_w_h - btn_show)/2.f, (btn_w_h - btn_show)/2.f))];
  115. [backBtn addTarget:self
  116. action:@selector(backBtnPressed)
  117. forControlEvents:(UIControlEventTouchUpInside)];
  118. // @synthesize titleLabel;
  119. titleLabel = [[UILabel alloc] init];
  120. [titleLabel setTextColor:HW0A132BColor];
  121. [titleLabel setFont:[UIFont boldSystemFontOfSize:18.f]];
  122. [navBarBGView addSubview:titleLabel];
  123. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  124. make.centerY.equalTo(backBtn.mas_centerY);
  125. make.left.mas_equalTo(60);
  126. make.right.mas_equalTo(-60);
  127. make.height.mas_equalTo(25);
  128. }];
  129. [titleLabel setTextAlignment:(NSTextAlignmentCenter)];
  130. }
  131. - (void)backBtnPressed{
  132. if (!self.canBack) {
  133. HLog(@"无法返回");
  134. return;
  135. }
  136. if (self.navigationController)
  137. {
  138. [self.navigationController popViewControllerAnimated:YES];
  139. }
  140. else
  141. {
  142. [self dismissViewControllerAnimated:YES completion:^{
  143. ;
  144. }];
  145. }
  146. }
  147. #pragma mark - HWSearchBarDelegate
  148. - (void)searchBarWithText:(NSString *)text {
  149. HLog(@"搜索:%@", text)
  150. }
  151. #pragma mark - HWToolBarDelegate
  152. - (void)backBtnDidClick {
  153. HLog(@"后退");
  154. }
  155. - (void)forwardBtnDidClick {
  156. HLog(@"前进");
  157. }
  158. - (void)toolBtnDidClick {
  159. HLog(@"工具");
  160. }
  161. - (void)homeBtnDidClick {
  162. HLog(@"主页");
  163. HWWebViewController *vc = [[HWWebViewController alloc] init];
  164. vc.webUrl = Const_HomeUrl;
  165. UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  166. UIWindow *keyWindow = [iTools getKeyWindow];
  167. keyWindow.rootViewController = nvc;
  168. }
  169. - (void)pageBtnDidClick {
  170. HLog(@"页面");
  171. // 1、更新截图
  172. [self updateBrowserImageWithWindow];
  173. // 2、跳转控制器
  174. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  175. HWPageViewController *vc = [[HWPageViewController alloc] init];
  176. [self.navigationController pushViewController:vc animated:YES];
  177. });
  178. }
  179. - (void)updateBrowserImageWithWindow {
  180. // 浏览器当前窗口索引ID
  181. NSInteger ID = [HWDataManager getIntegerWithKey:BrowserWindowsCurrentID];
  182. NSString *IDValue = [NSString stringWithFormat:@"%ld", ID];
  183. NSString *where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(IDValue)];
  184. NSArray *modelArr = [BaseModel bg_find:DB_BrowserWindows_TableName where:where];
  185. if (modelArr.count == 0) { // 当前窗口已被删除 更新BrowserWindowsCurrentID
  186. BaseModel *lastModel = [BaseModel bg_lastObject:DB_BrowserWindows_TableName];
  187. [HWDataManager setIntegerWithKey:BrowserWindowsCurrentID value:lastModel.ID];
  188. }else { // 当前窗口加载页面 更新model
  189. BaseModel *model = modelArr.firstObject;
  190. // 更新截图
  191. UIImage *image = [self getScreenShotImageFromBase];
  192. if (!image) {
  193. HLog(@"截图获取失败");
  194. return;
  195. }
  196. NSString *imageName = [NSString stringWithFormat:@"%ld", ID];
  197. NSString *imageUrl = [HWDataManager writeScreenShotImageToLocal:image withName:imageName];
  198. model.iconFile = imageUrl;
  199. // 更新数据库
  200. model.bg_tableName = DB_BrowserWindows_TableName;
  201. [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  202. HLog(@"BaseModel更新: %@", isSuccess ? @"成功":@"失败");
  203. }];
  204. }
  205. }
  206. /**View截图*/
  207. - (UIImage *)getScreenShotImageFromBase {
  208. CGSize size = self.view.frame.size;
  209. UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
  210. [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
  211. UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
  212. UIGraphicsEndImageContext();
  213. return viewImage;
  214. }
  215. #pragma mark loading
  216. -(void)showNewIndicatorWithCanBack:(BOOL)canBack canTouch:(BOOL)canTouch
  217. {
  218. needSowWattingView = YES;
  219. self.canTouch = canTouch;
  220. self.canBack = canBack;
  221. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  222. if (self->needSowWattingView)
  223. {
  224. if (self->wattingView == nil)
  225. {
  226. HLog(@"Tan +++++开");
  227. self->wattingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
  228. [self->wattingView setBackgroundColor:RGBACOLOR(0.f, 0.f, 0.f, 0.7)];
  229. [self->wattingView.layer setCornerRadius:10.f];
  230. self->wattingView.center = [iTools getKeyWindow].center;// 将wattingVideoImageindicator放于播放画面的中央位置
  231. self->wattingView.hidden = NO;
  232. FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"loadingWhiteGif" withExtension:@"gif"]]];
  233. // image.loopCount = 0; // 重复次数0 无限循环重复动画
  234. FLAnimatedImageView *upView = [[FLAnimatedImageView alloc] init];
  235. [upView setFrame:CGRectMake((100 - 65.f)/2.f, (100 - 65.f)/2.f, 65.f, 65.f)];
  236. [upView setContentMode:UIViewContentModeScaleAspectFill];
  237. [upView setBackgroundColor:[ UIColor clearColor]];
  238. upView.animatedImage = image;
  239. [self->wattingView addSubview:upView];
  240. [[iTools getKeyWindow] addSubview:self->wattingView];
  241. }
  242. }
  243. });
  244. }
  245. -(void)showNewIndicatorHaveStrWithCanBack:(BOOL)canBack canTouch:(BOOL)canTouch showText:(NSString *)text
  246. {
  247. needSowWattingViewStr = YES;
  248. self.canTouch = canTouch;
  249. self.canBack = canBack;
  250. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  251. if (self->needSowWattingViewStr)
  252. {
  253. if (self->wattingViewForHaveStr == nil)
  254. {
  255. HLog(@"Tan +++++开");
  256. self->wattingViewForHaveStr = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 135, 135)];
  257. [self->wattingViewForHaveStr setBackgroundColor:RGBACOLOR(0, 0, 0, 0.5)];
  258. [self->wattingViewForHaveStr.layer setCornerRadius:11.f];
  259. self->wattingViewForHaveStr.center = [iTools getKeyWindow].center;// 将wattingVideoImageindicator放于播放画面的中央位置
  260. self->wattingViewForHaveStr.hidden = NO;
  261. UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 90, 135-10, 40)];
  262. [contentLabel setBackgroundColor:[UIColor clearColor]];
  263. [contentLabel setText:text];
  264. [contentLabel setTextAlignment:(NSTextAlignmentCenter)];
  265. [contentLabel setTextColor:[UIColor whiteColor]];
  266. [contentLabel setFont:[UIFont systemFontOfSize:14]];
  267. contentLabel.numberOfLines = 0;
  268. [self->wattingViewForHaveStr addSubview:contentLabel];
  269. FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"loadingWhiteGif" withExtension:@"gif"]]];
  270. FLAnimatedImageView *upView = [[FLAnimatedImageView alloc] init];
  271. [upView setFrame:CGRectMake((135 - 59.f)/2.f, 25.f , 59.f, 59.f)];
  272. [upView setContentMode:UIViewContentModeScaleAspectFill];
  273. [upView setBackgroundColor:[ UIColor clearColor]];
  274. upView.animatedImage = image;
  275. [self->wattingViewForHaveStr addSubview:upView];
  276. [[iTools getKeyWindow] addSubview:self->wattingViewForHaveStr];
  277. }
  278. }
  279. });
  280. }
  281. -(void)removeNewIndicatorHaveStr
  282. {
  283. needSowWattingViewStr = NO;
  284. __weak __typeof(self) weakSelf = self;
  285. dispatch_async(dispatch_get_main_queue(), ^{
  286. self.canTouch = YES;
  287. self.canBack = YES;
  288. [weakSelf.view setUserInteractionEnabled:YES];
  289. if (self->wattingViewForHaveStr != nil)
  290. {
  291. [[self->wattingViewForHaveStr viewWithTag:1111].layer removeAllAnimations];
  292. self->wattingViewForHaveStr.hidden = YES;
  293. [self->wattingViewForHaveStr removeFromSuperview];
  294. self->wattingViewForHaveStr = nil;
  295. }
  296. });
  297. }
  298. -(void)removeNewIndicator
  299. {
  300. needSowWattingView = NO;
  301. __weak __typeof(self) weakSelf = self;
  302. dispatch_async(dispatch_get_main_queue(), ^{
  303. self.canTouch = YES;
  304. self.canBack = YES;
  305. [weakSelf.view setUserInteractionEnabled:YES];
  306. if (self->wattingView != nil)
  307. {
  308. [[self->wattingView viewWithTag:1111].layer removeAllAnimations];
  309. self->wattingView.hidden = YES;
  310. [self->wattingView removeFromSuperview];
  311. self->wattingView = nil;
  312. }
  313. });
  314. }
  315. #pragma mark 禁用和开始右滑返回手势
  316. - (void)enablePanRightBack
  317. {
  318. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  319. self.navigationController.interactivePopGestureRecognizer.enabled = YES;
  320. }
  321. if ([self.tabBarController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  322. self.tabBarController.navigationController.interactivePopGestureRecognizer.enabled = YES;
  323. }
  324. }
  325. - (void)noEnablePanRightBack
  326. {
  327. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  328. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  329. }
  330. if ([self.tabBarController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  331. self.tabBarController.navigationController.interactivePopGestureRecognizer.enabled = NO;
  332. }
  333. }
  334. @end