BaseViewController.m 12 KB

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