ZFPlayerStatusBar.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //
  2. // ZFPlayerStatusBar.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFPlayerStatusBar.h"
  25. #import <CoreTelephony/CTCarrier.h>
  26. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  27. #import "UIView+ZFFrame.h"
  28. #import "ZFReachabilityManager.h"
  29. #import "ZFUtilities.h"
  30. @interface ZFPlayerTimerTarget: NSProxy
  31. @property (nonatomic, weak) id target;
  32. @end
  33. @implementation ZFPlayerTimerTarget
  34. + (instancetype)proxyWithTarget:(id)target {
  35. ZFPlayerTimerTarget *proxy = [ZFPlayerTimerTarget alloc];
  36. proxy.target = target;
  37. return proxy;
  38. }
  39. - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
  40. NSMethodSignature *signature = nil;
  41. if ([self.target respondsToSelector:sel]) {
  42. signature = [self.target methodSignatureForSelector:sel];
  43. } else {
  44. /// 动态造一个 void object selector arg 函数签名。
  45. /// 目的是返回有效signature,不要因为找不到而crash
  46. signature = [NSMethodSignature signatureWithObjCTypes:"v@:@"];
  47. }
  48. return signature;
  49. }
  50. - (void)forwardInvocation:(NSInvocation *)invocation {
  51. if ([self.target respondsToSelector:invocation.selector]) {
  52. [invocation invokeWithTarget:self.target];
  53. }
  54. }
  55. @end
  56. @interface ZFPlayerStatusBar()
  57. /// 时间
  58. @property (nonatomic, strong) UILabel *dateLabel;
  59. /// 电池
  60. @property (nonatomic, strong) UIView *batteryView;
  61. /// 充电标识
  62. @property (nonatomic, strong) UIImageView *batteryImageView;
  63. /// 充电层
  64. @property (nonatomic, strong) CAShapeLayer *batteryLayer;
  65. /// 电池边框
  66. @property (nonatomic, strong) CAShapeLayer *batteryBoundLayer;
  67. /// 电池正极
  68. @property (nonatomic, strong) CAShapeLayer *batteryPositiveLayer;
  69. /// 电量百分比
  70. @property (nonatomic, strong) UILabel *batteryLabel;
  71. /// 网络状态
  72. @property (nonatomic, strong) UILabel *networkLabel;
  73. @property (nonatomic, strong) NSTimer *timer;
  74. @property (nonatomic, strong) NSDateFormatter *dateFormatter;
  75. @end
  76. @implementation ZFPlayerStatusBar
  77. - (instancetype)initWithFrame:(CGRect)frame {
  78. if (self = [super initWithFrame:frame]) {
  79. [self setup];
  80. }
  81. return self;
  82. }
  83. - (instancetype)initWithCoder:(NSCoder *)coder {
  84. if (self = [super initWithCoder:coder]) {
  85. [self setup];
  86. }
  87. return self;
  88. }
  89. - (void)layoutSubviews {
  90. [super layoutSubviews];
  91. [self.dateLabel sizeToFit];
  92. [self.networkLabel sizeToFit];
  93. [self.batteryLabel sizeToFit];
  94. self.dateLabel.zf_size = CGSizeMake(self.dateLabel.zf_width, 16);
  95. self.batteryView.frame = CGRectMake(self.bounds.size.width - 35 - (iPhoneX ? 44 : 0), 0, 22, 10);
  96. self.batteryLabel.frame = CGRectMake(self.batteryView.zf_x - 42, 0, self.batteryLabel.zf_width, 16);
  97. self.networkLabel.frame = CGRectMake(self.batteryLabel.zf_x - 40, 0, self.networkLabel.zf_width + 13, 14);
  98. self.dateLabel.center = self.center;
  99. self.batteryView.zf_centerY = self.zf_centerY;
  100. self.batteryLabel.zf_right = self.batteryView.zf_x - 5;
  101. self.batteryLabel.zf_centerY = self.batteryView.zf_centerY;
  102. self.networkLabel.zf_right = self.batteryLabel.zf_x - 10;
  103. self.networkLabel.zf_centerY = self.batteryView.zf_centerY;
  104. }
  105. - (void)dealloc {
  106. [self destoryTimer];
  107. }
  108. - (void)setup {
  109. self.refreshTime = 3.0;
  110. /// 时间
  111. [self addSubview:self.dateLabel];
  112. [self addSubview:self.batteryView];
  113. /// 电池
  114. [self.batteryView.layer addSublayer:self.batteryBoundLayer];
  115. /// 正极
  116. [self.batteryView.layer addSublayer:self.batteryPositiveLayer];
  117. /// 是否在充电
  118. [self.batteryView.layer addSublayer:self.batteryLayer];
  119. [self.batteryView addSubview:self.batteryImageView];
  120. [self addSubview:self.batteryLabel];
  121. [self addSubview:self.networkLabel];
  122. [UIDevice currentDevice].batteryMonitoringEnabled = YES;
  123. [[NSNotificationCenter defaultCenter] addObserver:self
  124. selector:@selector(batteryLevelDidChangeNotification:)
  125. name:UIDeviceBatteryLevelDidChangeNotification
  126. object:nil];
  127. [[NSNotificationCenter defaultCenter] addObserver:self
  128. selector:@selector(batteryStateDidChangeNotification:)
  129. name:UIDeviceBatteryStateDidChangeNotification
  130. object:nil];
  131. [[NSNotificationCenter defaultCenter] addObserver:self
  132. selector:@selector(localeDidChangeNotification:)
  133. name:NSCurrentLocaleDidChangeNotification
  134. object:nil];
  135. [[NSNotificationCenter defaultCenter] addObserver:self
  136. selector:@selector(networkDidChangeNotification:)
  137. name:ZFReachabilityDidChangeNotification
  138. object:nil];
  139. }
  140. - (void)batteryLevelDidChangeNotification:(NSNotification *)noti {
  141. [self updateUI];
  142. }
  143. - (void)batteryStateDidChangeNotification:(NSNotification *)noti {
  144. [self updateUI];
  145. }
  146. - (void)localeDidChangeNotification:(NSNotification *)noti {
  147. [self.dateFormatter setLocale:[NSLocale currentLocale]];
  148. [self updateUI];
  149. }
  150. - (void)networkDidChangeNotification:(NSNotification *)noti {
  151. self.networkLabel.text = [self networkStatus];
  152. [self setNeedsLayout];
  153. [self layoutIfNeeded];
  154. }
  155. - (void)startTimer {
  156. self.timer = [NSTimer timerWithTimeInterval:self.refreshTime target:[ZFPlayerTimerTarget proxyWithTarget:self] selector:@selector(updateUI) userInfo:nil repeats:YES];
  157. [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  158. [self.timer fire];
  159. }
  160. - (void)destoryTimer {
  161. if (self.timer) {
  162. [self.timer invalidate];
  163. self.timer = nil;
  164. }
  165. }
  166. #pragma mark - update UI
  167. - (void)updateUI {
  168. [self updateDate];
  169. [self updateBattery];
  170. [self setNeedsLayout];
  171. [self layoutIfNeeded];
  172. }
  173. - (void)updateDate {
  174. NSMutableString *dateString = [[NSMutableString alloc] initWithString:[self.dateFormatter stringFromDate:[NSDate date]]];
  175. NSRange amRange = [dateString rangeOfString:[self.dateFormatter AMSymbol]];
  176. NSRange pmRange = [dateString rangeOfString:[self.dateFormatter PMSymbol]];
  177. if (amRange.location != NSNotFound) {
  178. [dateString deleteCharactersInRange:amRange];
  179. } else if (pmRange.location != NSNotFound) {
  180. [dateString deleteCharactersInRange:pmRange];
  181. }
  182. self.dateLabel.text = dateString;
  183. }
  184. - (void)updateBattery {
  185. [UIDevice currentDevice].batteryMonitoringEnabled = YES;
  186. CGFloat batteryLevel = [UIDevice currentDevice].batteryLevel;
  187. /// -1是模拟器
  188. if (batteryLevel < 0) { batteryLevel = 1.0; }
  189. CGRect rect = CGRectMake(1.5, 1.5, (20-3)*batteryLevel, 10-3);
  190. UIBezierPath *batteryPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:2];
  191. UIColor *batteryColor;
  192. UIDeviceBatteryState batteryState = [UIDevice currentDevice].batteryState;
  193. if (batteryState == UIDeviceBatteryStateCharging || batteryState == UIDeviceBatteryStateFull) { /// 在充电
  194. self.batteryImageView.hidden = NO;
  195. } else {
  196. self.batteryImageView.hidden = YES;
  197. }
  198. if (@available(iOS 9.0, *)) {
  199. if ([NSProcessInfo processInfo].lowPowerModeEnabled) { /// 低电量模式
  200. batteryColor = UIColorFromHex(0xF9CF0E);
  201. } else {
  202. if (batteryState == UIDeviceBatteryStateCharging || batteryState == UIDeviceBatteryStateFull) { /// 在充电
  203. batteryColor = UIColorFromHex(0x37CB46);
  204. } else if (batteryLevel <= 0.2) { /// 电量低
  205. batteryColor = UIColorFromHex(0xF02C2D);
  206. } else { /// 电量正常 白色
  207. batteryColor = [UIColor whiteColor];
  208. }
  209. }
  210. } else {
  211. if (batteryState == UIDeviceBatteryStateCharging || batteryState == UIDeviceBatteryStateFull) { /// 在充电
  212. batteryColor = UIColorFromHex(0x37CB46);
  213. } else if (batteryLevel <= 0.2) { /// 电量低
  214. batteryColor = UIColorFromHex(0xF02C2D);
  215. } else { /// 电量正常 白色
  216. batteryColor = [UIColor whiteColor];
  217. }
  218. }
  219. self.batteryLayer.strokeColor = [UIColor clearColor].CGColor;
  220. self.batteryLayer.path = batteryPath.CGPath;
  221. self.batteryLayer.fillColor = batteryColor.CGColor;
  222. self.batteryLabel.text = [NSString stringWithFormat:@"%.0f%%", batteryLevel*100];
  223. }
  224. - (NSString *)networkStatus {
  225. NSString *net = @"WIFI";
  226. ZFReachabilityStatus netStatus = [ZFReachabilityManager sharedManager].networkReachabilityStatus;
  227. switch (netStatus) {
  228. case ZFReachabilityStatusReachableViaWiFi:
  229. net = @"WIFI";
  230. break;
  231. case ZFReachabilityStatusNotReachable:
  232. net = @"无网络";
  233. break;
  234. case ZFReachabilityStatusReachableVia2G:
  235. net = @"2G";
  236. break;
  237. case ZFReachabilityStatusReachableVia3G:
  238. net = @"3G";
  239. break;
  240. case ZFReachabilityStatusReachableVia4G:
  241. net = @"4G";
  242. break;
  243. case ZFReachabilityStatusReachableVia5G:
  244. net = @"5G";
  245. break;
  246. default:
  247. net = @"未知";
  248. break;
  249. }
  250. return net;
  251. }
  252. #pragma mark - getter
  253. - (UILabel *)dateLabel {
  254. if (!_dateLabel) {
  255. _dateLabel = [UILabel new];
  256. _dateLabel.bounds = CGRectMake(0, 0, 100, 16);
  257. _dateLabel.textColor = [UIColor whiteColor];
  258. _dateLabel.font = [UIFont systemFontOfSize:12];
  259. _dateLabel.textAlignment = NSTextAlignmentCenter;
  260. }
  261. return _dateLabel;
  262. }
  263. - (NSDateFormatter*)dateFormatter {
  264. if (!_dateFormatter) {
  265. _dateFormatter = [[NSDateFormatter alloc] init];
  266. [_dateFormatter setLocale:[NSLocale currentLocale]];
  267. [_dateFormatter setDateStyle:NSDateFormatterNoStyle];
  268. [_dateFormatter setTimeStyle:NSDateFormatterShortStyle];
  269. }
  270. return _dateFormatter;
  271. }
  272. - (UIView *)batteryView {
  273. if (!_batteryView) {
  274. _batteryView = [[UIView alloc] init];
  275. }
  276. return _batteryView;
  277. }
  278. - (UIImageView *)batteryImageView {
  279. if (!_batteryImageView) {
  280. _batteryImageView = [[UIImageView alloc] init];
  281. _batteryImageView.bounds = CGRectMake(0, 0, 8, 12);
  282. _batteryImageView.center = CGPointMake(10, 5);
  283. _batteryImageView.image = ZFPlayer_Image(@"ZFPlayer_battery_lightning");
  284. }
  285. return _batteryImageView;
  286. }
  287. - (CAShapeLayer *)batteryLayer {
  288. if (!_batteryLayer) {
  289. [UIDevice currentDevice].batteryMonitoringEnabled = YES;
  290. CGFloat batteryLevel = [UIDevice currentDevice].batteryLevel;
  291. UIBezierPath *batteryPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(1.5, 1.5, (20-3)*batteryLevel, 10-3) cornerRadius:2];
  292. _batteryLayer = [CAShapeLayer layer];
  293. _batteryLayer.lineWidth = 1;
  294. _batteryLayer.strokeColor = [UIColor clearColor].CGColor;
  295. _batteryLayer.path = batteryPath.CGPath;
  296. _batteryLayer.fillColor = [UIColor whiteColor].CGColor;
  297. }
  298. return _batteryLayer;
  299. }
  300. - (CAShapeLayer *)batteryBoundLayer {
  301. if (!_batteryBoundLayer) {
  302. UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 20, 10) cornerRadius:2.5];
  303. _batteryBoundLayer = [CAShapeLayer layer];
  304. _batteryBoundLayer.lineWidth = 1;
  305. _batteryBoundLayer.strokeColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8].CGColor;
  306. _batteryBoundLayer.path = bezierPath.CGPath;
  307. _batteryBoundLayer.fillColor = nil;
  308. }
  309. return _batteryBoundLayer;
  310. }
  311. - (CAShapeLayer *)batteryPositiveLayer {
  312. if (!_batteryPositiveLayer) {
  313. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(22, 3, 1, 3) byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight) cornerRadii:CGSizeMake(2, 2)];
  314. _batteryPositiveLayer = [CAShapeLayer layer];
  315. _batteryPositiveLayer.lineWidth = 0.5;
  316. _batteryPositiveLayer.strokeColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8].CGColor;
  317. _batteryPositiveLayer.path = path.CGPath;
  318. _batteryPositiveLayer.fillColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8].CGColor;
  319. }
  320. return _batteryPositiveLayer;
  321. }
  322. - (UILabel *)batteryLabel {
  323. if (!_batteryLabel) {
  324. _batteryLabel = [[UILabel alloc] init];
  325. _batteryLabel.textColor = [UIColor whiteColor];
  326. _batteryLabel.font = [UIFont systemFontOfSize:11];
  327. _batteryLabel.textAlignment = NSTextAlignmentRight;
  328. }
  329. return _batteryLabel;
  330. }
  331. - (UILabel *)networkLabel {
  332. if (!_networkLabel) {
  333. _networkLabel = [[UILabel alloc] init];
  334. _networkLabel.layer.cornerRadius = 7;
  335. _networkLabel.layer.borderWidth = 1;
  336. _networkLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
  337. _networkLabel.textColor = [UIColor whiteColor];
  338. _networkLabel.font = [UIFont systemFontOfSize:9];
  339. _networkLabel.textAlignment = NSTextAlignmentCenter;
  340. _networkLabel.text = @"WIFI";
  341. }
  342. return _networkLabel;
  343. }
  344. @end