ProgressBackView.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //
  2. // ProgressBackView.m
  3. // XMEye
  4. //
  5. // Created by XM on 2017/3/11.
  6. // Copyright © 2017年 Megatron. All rights reserved.
  7. //
  8. #import "ProgressBackView.h"
  9. #import "FunSDK/FunSDK.h"
  10. #import "NSDate+TimeCategory.h"
  11. #import "Header.h"
  12. @implementation ProgressBackView
  13. {
  14. NSTimer *_timer; //拖动时间轴时候的定时器
  15. NSInteger _percens; //毫秒
  16. CGPoint _start_Point; // 上一次移动的点
  17. int _countNum; //这个参数原先是用来判断是否要调用拖动时间轴接口的
  18. BOOL _canUpdateTime; //这个参数原先是用来判断是否要调用拖动时间轴接口的
  19. }
  20. -(id)initWithFrame:(CGRect)frame
  21. {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. //创建时间轴
  25. self.MPView = [[MyProgressView alloc] initWithFrame:CGRectMake(0, 44, ScreenWidth, 100)];
  26. self.MPView.backgroundColor = [UIColor clearColor];
  27. self.MPView.num = 0;
  28. self.MPView.dataArray = nil;
  29. self.MPView.userInteractionEnabled = YES;
  30. self.MPView.type = UNIT_TYPE_MINUTE;
  31. [self addSubview:self.MPView];
  32. //回访界面时间轴中间红线
  33. pView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth * 0.5 -1.5, 50, 3, 80)];
  34. pView.backgroundColor = [UIColor colorWithRed:255/255.0 green:67/255.0 blue:76/255.0 alpha:1];
  35. pView.hidden = YES;
  36. [self addSubview:pView];
  37. //时间单位
  38. UILabel *labUnit = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 200, frame.size.height - 40, 100, 30)];
  39. labUnit.text = TS("Unit");
  40. labUnit.textAlignment = NSTextAlignmentRight;
  41. [self addSubview:labUnit];
  42. [self addSubview:self.labTime];
  43. [self addSubview:self.control];
  44. self.userInteractionEnabled = YES;
  45. }
  46. return self;
  47. }
  48. #pragma mark - 录像回放时间帧回调,用来刷新label时间显示和时间轴
  49. time_t ToTime_t(SDK_SYSTEM_TIME *pDvrTime);
  50. -(void)refreshTimeAndProgress:(int)timeInfo
  51. {
  52. //刷新时间轴
  53. SDK_SYSTEM_TIME time = {0};
  54. time.year = [NSDate getYearFormDate:_date];
  55. time.month = [NSDate getMonthFormDate:_date];
  56. time.day = [NSDate getDayFormDate:_date];
  57. time.hour = 0;
  58. time.second = 0;
  59. time.minute = 0;
  60. int thisTime = (int)ToTime_t(&time);
  61. _add = timeInfo - thisTime;
  62. [self refreshProgress];
  63. //刷新时间label
  64. if (_add == 0) {
  65. return;
  66. }
  67. _labTime.text = [self getSecondStringWithSecond:_add];
  68. }
  69. #pragma mark - 通过搜索录像返回的时间来刷新时间轴
  70. -(void)refreshWithAddTime:(NSInteger)add
  71. {
  72. pView.hidden = NO;
  73. _MPView.middleT = add;
  74. _add = add;
  75. if (_MPView == nil || _MPView.dataArray == nil || _MPView.dataArray.count == 0) {
  76. return;
  77. }
  78. [self refreshProgress];
  79. }
  80. #pragma mark -根据返回的录像文件刷新时间轴
  81. -(void)refreshProgressWithSearchResult:(NSMutableArray*)array
  82. {
  83. if (_MPView) {
  84. _MPView.dataArray = [array mutableCopy];
  85. [self refreshProgress];
  86. }
  87. }
  88. -(void)refreshProgress
  89. {
  90. [self showProgressWithUnit:_MPView.type andMiddleSecond:_add];
  91. [_MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:_MPView.type];
  92. [_MPView setNeedsDisplay];
  93. }
  94. #pragma mark - 根据回放时间_add刷新时间轴
  95. -(void)showProgressWithUnit:(Unit_Type)type andMiddleSecond:(float)second
  96. {
  97. if(_MPView.dataArray.count == 0) {
  98. return ;
  99. }
  100. if (UNIT_TYPE_HOUR == type) {
  101. for (int i = 0; i < _MPView.dataArray.count; i ++) {
  102. TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
  103. if (info.start_Time <= second && second <= info.end_Time) {
  104. _standardNum = i;
  105. break;
  106. }
  107. }
  108. for (int i = _standardNum; i >= 0; i --) {
  109. TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
  110. if ( ((second - info.end_Time) / (3600.0 * 24) * Unit_Hour * 24) > (ScreenWidth * 0.5)) {
  111. _leftNum = i;
  112. break;
  113. }
  114. if (i == 0)
  115. {
  116. _leftNum = 0;
  117. }
  118. }
  119. for (int i = _standardNum; i < _MPView.dataArray.count; i ++) {
  120. TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
  121. if (((info.start_Time - second) / 3600.0 * Unit_Hour) > (ScreenWidth * 0.5)) {
  122. _rightNum = i;
  123. break;
  124. }
  125. }
  126. }
  127. else if (UNIT_TYPE_MINUTE == type)
  128. {
  129. for (int i = 0; i < _MPView.dataArray.count; i ++) {
  130. TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
  131. if (info.start_Time <= second && second <= info.end_Time) {
  132. _standardNum = i;
  133. break;
  134. }
  135. }
  136. for (int i = _standardNum; i >= 0; i --) {
  137. TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
  138. float myWidth = ((second - info.end_Time) / (3600.0 * 24) * Unit_Minute * 24 * 6);
  139. float halfWidth = ScreenWidth * 0.5;
  140. if ( myWidth > halfWidth) {
  141. _leftNum = i;
  142. break;
  143. }
  144. if (i == 0)
  145. {
  146. _leftNum = 0;
  147. }
  148. }
  149. for (int i = _standardNum; i < _MPView.dataArray.count; i ++) {
  150. TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
  151. float myWidth = ((info.start_Time - second) / (3600.0 * 24) * Unit_Minute * 24 * 6);
  152. float halfWidth = (ScreenWidth * 0.5);
  153. if ( myWidth > halfWidth ) {
  154. _rightNum = i;
  155. break;
  156. }
  157. }
  158. }
  159. else if (UNIT_TYPE_SECOND == type)
  160. {
  161. for (int i = 0; i < _MPView.dataArray.count; i ++) {
  162. TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
  163. if (info.start_Time <= second && second <= info.end_Time) {
  164. _standardNum = i;
  165. break;
  166. }
  167. }
  168. for (int i = _standardNum; i >= 0; i --) {
  169. TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
  170. if ( ((second - info.start_Time) / (3600.0 * 24) * Unit_Second * 60 * 24 * 6) > (ScreenWidth * 0.5)) {
  171. _leftNum = i;
  172. break;
  173. }
  174. if (i == 0)
  175. {
  176. _leftNum = 0;
  177. }
  178. }
  179. for (int i = _standardNum; i < _MPView.dataArray.count; i ++) {
  180. TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
  181. if (((info.end_Time - second) / (3600.0 * 24) * Unit_Second * 60 * 24 * 6) > (ScreenWidth * 0.5)) {
  182. _rightNum = i;
  183. break;
  184. }
  185. }
  186. }
  187. }
  188. #pragma mark - UITouchEvent 触摸事件
  189. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  190. {
  191. _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];
  192. _percens = 0;
  193. _start_Point = [[touches anyObject] locationInView:self];
  194. if (_start_Point.y >= 50) {
  195. _countNum = 0;
  196. _ifSliding = YES;
  197. _canUpdateTime = NO;
  198. }
  199. }
  200. -(void)startTimer
  201. {
  202. _percens++;
  203. }
  204. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  205. {
  206. if (_ifSliding == YES) {
  207. if (_percens >= 20) {
  208. CGPoint point = [[touches anyObject] locationInView:self];
  209. float number = - (point.x - _start_Point.x);
  210. [self updateDataWithFloatNumber:number];
  211. _start_Point = point;
  212. _labTime.text = [self getSecondStringWithSecond:_add];
  213. }
  214. }
  215. }
  216. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  217. {
  218. if (_ifSliding == YES) {
  219. if (_percens < 20) {
  220. CGPoint point = [[touches anyObject] locationInView:self];
  221. float number = 0.0;
  222. if (point.x < _start_Point.x) {
  223. if (self.MPView.type == UNIT_TYPE_SECOND) {
  224. number = 180.0;
  225. }
  226. if (self.MPView.type == UNIT_TYPE_MINUTE) {
  227. number = 3.0;
  228. }
  229. if (self.MPView.type == UNIT_TYPE_HOUR) {
  230. number = 0.5;
  231. }
  232. } else {
  233. if (self.MPView.type == UNIT_TYPE_SECOND) {
  234. number = -180.0;
  235. }
  236. if (self.MPView.type == UNIT_TYPE_MINUTE) {
  237. number = -3.0;
  238. }
  239. if (self.MPView.type == UNIT_TYPE_HOUR) {
  240. number = -0.5;
  241. }
  242. }
  243. [self updateDataWithFloatNumber:number];
  244. _labTime.text = [self getSecondStringWithSecond:_add];
  245. }
  246. NSString *current = _labTime.text;
  247. NSRange range1 = NSMakeRange(0, 2);
  248. NSRange range2 = NSMakeRange(3, 2);
  249. NSRange range3 = NSMakeRange(6, 2);
  250. _add = [[current substringWithRange:range1] integerValue] * 60 * 60 + [[current substringWithRange:range2] integerValue] * 60 + [[current substringWithRange:range3] integerValue];
  251. //调用接口进行跳转,并且界面刷新
  252. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(seekToTime) object:nil];
  253. [self performSelector:@selector(seekToTime) withObject:nil afterDelay:1.0];
  254. }
  255. [_timer invalidate];
  256. }
  257. -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  258. {
  259. _ifSliding = NO;
  260. _canUpdateTime = YES;
  261. }
  262. -(void)seekToTime
  263. {
  264. if (self.TouchSeektoTime) {
  265. self.TouchSeektoTime(_add);
  266. }
  267. _ifSliding = NO;
  268. _canUpdateTime = YES;
  269. }
  270. //拖动时间轴时候的处理
  271. -(void)updateDataWithFloatNumber:(float)number
  272. {
  273. if (self.MPView.type == UNIT_TYPE_HOUR) {
  274. float time = number / (24 * Unit_Hour) * 24 * 60 * 60;
  275. _add = _add + time;
  276. if (_add >= 0 && _add <= (24*60*60)) {
  277. [self showProgressWithUnit:UNIT_TYPE_HOUR andMiddleSecond:_add];
  278. [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
  279. }
  280. else if ( _add < 0)
  281. {
  282. [self showProgressWithUnit:UNIT_TYPE_HOUR andMiddleSecond:0];
  283. [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
  284. _add = 0;
  285. }
  286. else if (_add > (24*60*60))
  287. {
  288. [self showProgressWithUnit:UNIT_TYPE_HOUR andMiddleSecond:(24*60*60)];
  289. [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
  290. _add = 24*60*60;
  291. }
  292. }
  293. else if (self.MPView.type == UNIT_TYPE_MINUTE)
  294. {
  295. float time = number / (24 * 6 * Unit_Minute) * 24 * 60 * 60;
  296. _add = _add + time;
  297. if (_add >= 0 && _add <= (24*60*60)) {
  298. [self showProgressWithUnit:UNIT_TYPE_MINUTE andMiddleSecond:_add];
  299. [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
  300. }
  301. else if ( _add < 0)
  302. {
  303. [self showProgressWithUnit:UNIT_TYPE_MINUTE andMiddleSecond:0];
  304. [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
  305. _add = 0;
  306. }
  307. else if (_add > (24*60*60))
  308. {
  309. [self showProgressWithUnit:UNIT_TYPE_MINUTE andMiddleSecond:(24*60*60)];
  310. [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
  311. _add = 24*60*60;
  312. }
  313. }
  314. else if (self.MPView.type == UNIT_TYPE_SECOND)
  315. {
  316. float time = number / (24 * 6 * Unit_Second * 60) * 24 * 60 * 60;
  317. _add = _add + time;
  318. if (_add >= 0 && _add <= (24*60*60)) {
  319. [self showProgressWithUnit:UNIT_TYPE_SECOND andMiddleSecond:_add];
  320. [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
  321. }
  322. else if ( _add < 0)
  323. {
  324. [self showProgressWithUnit:UNIT_TYPE_SECOND andMiddleSecond:0];
  325. [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
  326. _add = 0;
  327. }
  328. else if (_add > (24*60*60))
  329. {
  330. [self showProgressWithUnit:UNIT_TYPE_SECOND andMiddleSecond:(24*60*60)];
  331. [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
  332. _add = 24*60*60;
  333. }
  334. }
  335. }
  336. // 秒数转为时分秒
  337. -(NSString *)getSecondStringWithSecond:(int)second
  338. {
  339. int h = second / 3600;
  340. int m = (second % 3600) / 60;
  341. int s = ((second % 3600) % 60);
  342. if (s<0) {
  343. s=0;
  344. }
  345. NSString *str = [NSString stringWithFormat:@"%@:%@:%@",h>=10?[NSString stringWithFormat:@"%i",h]:[NSString stringWithFormat:@"0%i",h],m>=10?[NSString stringWithFormat:@"%i",m]:[NSString stringWithFormat:@"0%i",m],s>=10?[NSString stringWithFormat:@"%i",s]:[NSString stringWithFormat:@"0%i",s]];
  346. return str;
  347. }
  348. #pragma mark - 点击事件单位,修改时间单位
  349. -(void)segValueChanged:(UISegmentedControl*)control
  350. {
  351. if (self.MPView == nil) {
  352. return;
  353. }
  354. NSInteger num = control.selectedSegmentIndex;
  355. if (num == 0) {
  356. self.MPView.type = UNIT_TYPE_HOUR;
  357. }
  358. else if (num == 1)
  359. {
  360. self.MPView.type = UNIT_TYPE_MINUTE;
  361. }
  362. else
  363. {
  364. self.MPView.type = UNIT_TYPE_SECOND;
  365. }
  366. }
  367. #pragma mark - 创建时间和单位控制器
  368. -(UILabel*)labTime
  369. {
  370. _labTime = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
  371. _labTime.center = CGPointMake(ScreenWidth * 0.5, 30);
  372. _labTime.backgroundColor = [UIColor clearColor];
  373. _labTime.textColor = NormalFontColor;
  374. _labTime.textAlignment = NSTextAlignmentCenter;
  375. _labTime.font = [UIFont systemFontOfSize:12.0];
  376. return _labTime;
  377. }
  378. -(UISegmentedControl*)control
  379. {
  380. NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2", nil];
  381. _control = [[UISegmentedControl alloc] initWithItems:array];
  382. _control.frame = CGRectMake(ScreenWidth - 90, self.frame.size.height - 40, 70, 30);
  383. [_control setTitle:TS("h") forSegmentAtIndex:0];
  384. [_control setTitle:TS("m") forSegmentAtIndex:1];
  385. [_control addTarget:self action:@selector(segValueChanged:) forControlEvents:UIControlEventValueChanged];
  386. _control.selectedSegmentIndex = 1;
  387. [self addSubview:_control];
  388. return _control;
  389. }
  390. -(void)dealloc
  391. {
  392. if (_MPView) {
  393. _MPView = nil;
  394. }
  395. if (pView) {
  396. pView = nil;
  397. }
  398. if (_labTime) {
  399. _labTime = nil;
  400. }
  401. }
  402. @end