BaseViewController.m 12 KB

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