| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- //
- // ProgressBackView.m
- // XMEye
- //
- // Created by XM on 2017/3/11.
- // Copyright © 2017年 Megatron. All rights reserved.
- //
- #import "ProgressBackView.h"
- #import "FunSDK/FunSDK.h"
- #import "NSDate+TimeCategory.h"
- #import "Header.h"
- @implementation ProgressBackView
- {
- NSTimer *_timer; //拖动时间轴时候的定时器
- NSInteger _percens; //毫秒
- CGPoint _start_Point; // 上一次移动的点
- int _countNum; //这个参数原先是用来判断是否要调用拖动时间轴接口的
- BOOL _canUpdateTime; //这个参数原先是用来判断是否要调用拖动时间轴接口的
- }
- -(id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- //创建时间轴
- self.MPView = [[MyProgressView alloc] initWithFrame:CGRectMake(0, 44, ScreenWidth, 100)];
- self.MPView.backgroundColor = [UIColor clearColor];
- self.MPView.num = 0;
- self.MPView.dataArray = nil;
- self.MPView.userInteractionEnabled = YES;
- self.MPView.type = UNIT_TYPE_MINUTE;
- [self addSubview:self.MPView];
- //回访界面时间轴中间红线
- pView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth * 0.5 -1.5, 50, 3, 80)];
- pView.backgroundColor = [UIColor colorWithRed:255/255.0 green:67/255.0 blue:76/255.0 alpha:1];
- pView.hidden = YES;
- [self addSubview:pView];
- //时间单位
- UILabel *labUnit = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 200, frame.size.height - 40, 100, 30)];
- labUnit.text = TS("Unit");
- labUnit.textAlignment = NSTextAlignmentRight;
- [self addSubview:labUnit];
- [self addSubview:self.labTime];
- [self addSubview:self.control];
- self.userInteractionEnabled = YES;
- }
- return self;
- }
- #pragma mark - 录像回放时间帧回调,用来刷新label时间显示和时间轴
- time_t ToTime_t(SDK_SYSTEM_TIME *pDvrTime);
- -(void)refreshTimeAndProgress:(int)timeInfo
- {
- //刷新时间轴
- SDK_SYSTEM_TIME time = {0};
- time.year = [NSDate getYearFormDate:_date];
- time.month = [NSDate getMonthFormDate:_date];
- time.day = [NSDate getDayFormDate:_date];
- time.hour = 0;
- time.second = 0;
- time.minute = 0;
- int thisTime = (int)ToTime_t(&time);
- _add = timeInfo - thisTime;
- [self refreshProgress];
- //刷新时间label
- if (_add == 0) {
- return;
- }
- _labTime.text = [self getSecondStringWithSecond:_add];
- }
- #pragma mark - 通过搜索录像返回的时间来刷新时间轴
- -(void)refreshWithAddTime:(NSInteger)add
- {
- pView.hidden = NO;
- _MPView.middleT = add;
- _add = add;
- if (_MPView == nil || _MPView.dataArray == nil || _MPView.dataArray.count == 0) {
- return;
- }
- [self refreshProgress];
- }
- #pragma mark -根据返回的录像文件刷新时间轴
- -(void)refreshProgressWithSearchResult:(NSMutableArray*)array
- {
- if (_MPView) {
- _MPView.dataArray = [array mutableCopy];
- [self refreshProgress];
- }
- }
- -(void)refreshProgress
- {
- [self showProgressWithUnit:_MPView.type andMiddleSecond:_add];
- [_MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:_MPView.type];
- [_MPView setNeedsDisplay];
- }
- #pragma mark - 根据回放时间_add刷新时间轴
- -(void)showProgressWithUnit:(Unit_Type)type andMiddleSecond:(float)second
- {
- if(_MPView.dataArray.count == 0) {
- return ;
- }
-
- if (UNIT_TYPE_HOUR == type) {
- for (int i = 0; i < _MPView.dataArray.count; i ++) {
- TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
- if (info.start_Time <= second && second <= info.end_Time) {
- _standardNum = i;
- break;
- }
- }
- for (int i = _standardNum; i >= 0; i --) {
- TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
- if ( ((second - info.end_Time) / (3600.0 * 24) * Unit_Hour * 24) > (ScreenWidth * 0.5)) {
- _leftNum = i;
- break;
- }
- if (i == 0)
- {
- _leftNum = 0;
- }
- }
- for (int i = _standardNum; i < _MPView.dataArray.count; i ++) {
- TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
- if (((info.start_Time - second) / 3600.0 * Unit_Hour) > (ScreenWidth * 0.5)) {
- _rightNum = i;
- break;
- }
- }
- }
- else if (UNIT_TYPE_MINUTE == type)
- {
- for (int i = 0; i < _MPView.dataArray.count; i ++) {
- TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
- if (info.start_Time <= second && second <= info.end_Time) {
- _standardNum = i;
- break;
- }
- }
- for (int i = _standardNum; i >= 0; i --) {
- TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
- float myWidth = ((second - info.end_Time) / (3600.0 * 24) * Unit_Minute * 24 * 6);
- float halfWidth = ScreenWidth * 0.5;
- if ( myWidth > halfWidth) {
- _leftNum = i;
- break;
- }
- if (i == 0)
- {
- _leftNum = 0;
- }
- }
- for (int i = _standardNum; i < _MPView.dataArray.count; i ++) {
- TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
- float myWidth = ((info.start_Time - second) / (3600.0 * 24) * Unit_Minute * 24 * 6);
- float halfWidth = (ScreenWidth * 0.5);
- if ( myWidth > halfWidth ) {
- _rightNum = i;
- break;
- }
- }
- }
- else if (UNIT_TYPE_SECOND == type)
- {
- for (int i = 0; i < _MPView.dataArray.count; i ++) {
- TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
- if (info.start_Time <= second && second <= info.end_Time) {
- _standardNum = i;
- break;
- }
- }
- for (int i = _standardNum; i >= 0; i --) {
- TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
- if ( ((second - info.start_Time) / (3600.0 * 24) * Unit_Second * 60 * 24 * 6) > (ScreenWidth * 0.5)) {
- _leftNum = i;
- break;
- }
- if (i == 0)
- {
- _leftNum = 0;
- }
- }
- for (int i = _standardNum; i < _MPView.dataArray.count; i ++) {
- TimeInfo *info = [_MPView.dataArray objectAtIndex:i];
- if (((info.end_Time - second) / (3600.0 * 24) * Unit_Second * 60 * 24 * 6) > (ScreenWidth * 0.5)) {
- _rightNum = i;
- break;
- }
- }
- }
- }
- #pragma mark - UITouchEvent 触摸事件
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];
- _percens = 0;
- _start_Point = [[touches anyObject] locationInView:self];
- if (_start_Point.y >= 50) {
- _countNum = 0;
- _ifSliding = YES;
- _canUpdateTime = NO;
- }
- }
- -(void)startTimer
- {
- _percens++;
- }
- -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if (_ifSliding == YES) {
- if (_percens >= 20) {
- CGPoint point = [[touches anyObject] locationInView:self];
- float number = - (point.x - _start_Point.x);
- [self updateDataWithFloatNumber:number];
- _start_Point = point;
- _labTime.text = [self getSecondStringWithSecond:_add];
- }
- }
- }
- -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if (_ifSliding == YES) {
- if (_percens < 20) {
- CGPoint point = [[touches anyObject] locationInView:self];
- float number = 0.0;
- if (point.x < _start_Point.x) {
- if (self.MPView.type == UNIT_TYPE_SECOND) {
- number = 180.0;
- }
- if (self.MPView.type == UNIT_TYPE_MINUTE) {
- number = 3.0;
- }
- if (self.MPView.type == UNIT_TYPE_HOUR) {
- number = 0.5;
- }
- } else {
- if (self.MPView.type == UNIT_TYPE_SECOND) {
- number = -180.0;
- }
- if (self.MPView.type == UNIT_TYPE_MINUTE) {
- number = -3.0;
- }
- if (self.MPView.type == UNIT_TYPE_HOUR) {
- number = -0.5;
- }
- }
- [self updateDataWithFloatNumber:number];
- _labTime.text = [self getSecondStringWithSecond:_add];
- }
- NSString *current = _labTime.text;
- NSRange range1 = NSMakeRange(0, 2);
- NSRange range2 = NSMakeRange(3, 2);
- NSRange range3 = NSMakeRange(6, 2);
-
- _add = [[current substringWithRange:range1] integerValue] * 60 * 60 + [[current substringWithRange:range2] integerValue] * 60 + [[current substringWithRange:range3] integerValue];
- //调用接口进行跳转,并且界面刷新
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(seekToTime) object:nil];
- [self performSelector:@selector(seekToTime) withObject:nil afterDelay:1.0];
- }
-
- [_timer invalidate];
- }
- -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- {
- _ifSliding = NO;
- _canUpdateTime = YES;
- }
- -(void)seekToTime
- {
- if (self.TouchSeektoTime) {
- self.TouchSeektoTime(_add);
- }
- _ifSliding = NO;
- _canUpdateTime = YES;
- }
- //拖动时间轴时候的处理
- -(void)updateDataWithFloatNumber:(float)number
- {
- if (self.MPView.type == UNIT_TYPE_HOUR) {
- float time = number / (24 * Unit_Hour) * 24 * 60 * 60;
- _add = _add + time;
- if (_add >= 0 && _add <= (24*60*60)) {
- [self showProgressWithUnit:UNIT_TYPE_HOUR andMiddleSecond:_add];
- [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
- }
- else if ( _add < 0)
- {
- [self showProgressWithUnit:UNIT_TYPE_HOUR andMiddleSecond:0];
- [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
- _add = 0;
- }
- else if (_add > (24*60*60))
- {
- [self showProgressWithUnit:UNIT_TYPE_HOUR andMiddleSecond:(24*60*60)];
- [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
- _add = 24*60*60;
- }
- }
- else if (self.MPView.type == UNIT_TYPE_MINUTE)
- {
- float time = number / (24 * 6 * Unit_Minute) * 24 * 60 * 60;
- _add = _add + time;
- if (_add >= 0 && _add <= (24*60*60)) {
- [self showProgressWithUnit:UNIT_TYPE_MINUTE andMiddleSecond:_add];
- [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
- }
- else if ( _add < 0)
- {
- [self showProgressWithUnit:UNIT_TYPE_MINUTE andMiddleSecond:0];
- [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
- _add = 0;
- }
- else if (_add > (24*60*60))
- {
- [self showProgressWithUnit:UNIT_TYPE_MINUTE andMiddleSecond:(24*60*60)];
- [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
- _add = 24*60*60;
- }
- }
- else if (self.MPView.type == UNIT_TYPE_SECOND)
- {
- float time = number / (24 * 6 * Unit_Second * 60) * 24 * 60 * 60;
- _add = _add + time;
- if (_add >= 0 && _add <= (24*60*60)) {
- [self showProgressWithUnit:UNIT_TYPE_SECOND andMiddleSecond:_add];
- [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
- }
- else if ( _add < 0)
- {
- [self showProgressWithUnit:UNIT_TYPE_SECOND andMiddleSecond:0];
- [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
- _add = 0;
- }
- else if (_add > (24*60*60))
- {
- [self showProgressWithUnit:UNIT_TYPE_SECOND andMiddleSecond:(24*60*60)];
- [self.MPView refreshViewWithDataFrom:_leftNum to:_rightNum andMiddleNum:_standardNum andMiddleTime:_add andType:self.MPView.type];
- _add = 24*60*60;
- }
- }
- }
- // 秒数转为时分秒
- -(NSString *)getSecondStringWithSecond:(int)second
- {
- int h = second / 3600;
- int m = (second % 3600) / 60;
- int s = ((second % 3600) % 60);
- if (s<0) {
- s=0;
- }
- 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]];
- return str;
- }
- #pragma mark - 点击事件单位,修改时间单位
- -(void)segValueChanged:(UISegmentedControl*)control
- {
- if (self.MPView == nil) {
- return;
- }
- NSInteger num = control.selectedSegmentIndex;
- if (num == 0) {
- self.MPView.type = UNIT_TYPE_HOUR;
- }
- else if (num == 1)
- {
- self.MPView.type = UNIT_TYPE_MINUTE;
- }
- else
- {
- self.MPView.type = UNIT_TYPE_SECOND;
- }
- }
- #pragma mark - 创建时间和单位控制器
- -(UILabel*)labTime
- {
- _labTime = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
- _labTime.center = CGPointMake(ScreenWidth * 0.5, 30);
- _labTime.backgroundColor = [UIColor clearColor];
- _labTime.textColor = NormalFontColor;
- _labTime.textAlignment = NSTextAlignmentCenter;
- _labTime.font = [UIFont systemFontOfSize:12.0];
- return _labTime;
- }
- -(UISegmentedControl*)control
- {
-
- NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2", nil];
- _control = [[UISegmentedControl alloc] initWithItems:array];
- _control.frame = CGRectMake(ScreenWidth - 90, self.frame.size.height - 40, 70, 30);
- [_control setTitle:TS("h") forSegmentAtIndex:0];
- [_control setTitle:TS("m") forSegmentAtIndex:1];
- [_control addTarget:self action:@selector(segValueChanged:) forControlEvents:UIControlEventValueChanged];
- _control.selectedSegmentIndex = 1;
- [self addSubview:_control];
- return _control;
- }
- -(void)dealloc
- {
- if (_MPView) {
- _MPView = nil;
- }
- if (pView) {
- pView = nil;
- }
- if (_labTime) {
- _labTime = nil;
- }
- }
- @end
|