wxr
2020-06-16 f6fd8acd7c53c44187e70b4709443318a628f4b5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//
//  HDLOACCurtainSeekBar.m
//  HDL_Widget_iOS
//
//  Created by HDL on 2019/11/6.
//  Copyright © 2019 JLChen. All rights reserved.
//
 
#import "HDLOACCurtainSeekBar.h"
#import "HDLUtlisXM.h"
//
//#define APP_DEFAULT_BG      HEXCOLOR(0xE5E5ED)
////#define APP_DEFAULT_BG_LINE      [UIColor colorWithRed:73/255.0f green:87/255.0f blue:128/255.0f alpha:1.0f]
//#define APP_DEFAULT_BAR        HEXCOLOR(0xCBCEE5)
 
#define APP_DEFAULT_OFFLINE_COLOR       HEXCOLORA(0xE9E9EC,0.8)
 
@interface HDLOACCurtainSeekBar ()
 
 
 
/**
 是否正在移动
 */
@property (nonatomic, assign) BOOL bTouchMove;
 
/**
 进度单位符号
 */
@property (nonatomic, strong) NSString *mProgressBarUnitSring;
 
/**
 最小值
 */
@property (nonatomic, assign) float mMinValue;
 
/**
 最大值
 */
@property (nonatomic, assign) float mMaxValue;
 
 
/**
 边距
 */
@property (nonatomic, assign) int CurtainPadding;
 
/**
 
 */
//@property (nonatomic, strong) UIImageView *bgImageView;
 
 
 
/**
 顶部padding
 */
@property (nonatomic, assign) int mCurtainPaddingTop;
 
/**
 是否离线
 */
@property (nonatomic, assign) BOOL isOffline;
 
 
@end
 
 
@implementation HDLOACCurtainSeekBar{
    
    int _mMoveCount;
    
    CGPoint _mCenterPoint;  //圆心坐标
    
    bool _bIsInCurtainProgress;
    
    CGFloat _mTopImageHeight;
    CGFloat _mCurtainHeight;
    CGFloat _mCurtainWidth;
    CGFloat _mCurtainWidthHalf;
    
    CGFloat _mCurtainStartX;
    CGFloat _mCurtainEndX;
    
    float _mPercent;
    
    CGFloat _mCurtainPadding;
    CGFloat _mButtonWidth;
    CGFloat _mCurtainX;
    CGFloat _mCornerRadius;
    UIImage *_curtainImage;
    UIImage *_bgImage;
    UIImage *_bgTopImage;
    BOOL bDownLeft;
    
}
 
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _isClickable = YES;
        _mMoveCount = 0;
        
        _mProgress = 0;
        _mPercent = 0;
        _mMaxValue = 100.0f;
        _mMinValue = 0.0f;
        _mCornerRadius = 0;
        _CurtainPadding = 15;
        _mProgressTextColor = [UIColor blackColor];
        _mProgressTextSize = 15;
        _mProgressBarUnitSring = @"%";
        _isProgressTextShow = YES;
        _mCurtainPaddingTop = 5;
//        _mTopImageHeight = 18;
        
        [self refreshFrame];
        _curtainImage = [UIImage imageNamed:@"ic_wd_curtain_h_progress"];
        self.backgroundColor = [UIColor clearColor];
        //        _bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        //        [_bgImageView setImage:[UIImage imageNamed:@"ic_wd_curtain_h_bg"]];
        //        [self addSubview:_bgImageView];
        
        _bgImage = [UIImage imageNamed:@"ic_wd_curtain_h_bg"];
        _bgTopImage =[UIImage imageNamed:@"ic_wd_curtain_h_top"];
    }
    
    return  self;
}
 
 
 
/**
 刷新布局,更新关键值
 
 */
-(void)refreshFrame{
    _mCenterPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    [self onCurtainSizeChange];
}
 
/**
 刷新布局,更新关键值
 
 */
-(void)onCurtainSizeChange{
    _mCurtainPadding = _CurtainPadding;
    
    _mCurtainWidth = self.frame.size.width - _mCurtainPadding*2;
    _mCurtainStartX = _mCurtainPadding;
    _mCurtainEndX = _mCurtainWidth + _mCurtainStartX;
    _mButtonWidth = _mCurtainWidth / 6;
    _mCurtainWidthHalf = _mCurtainWidth/2;
    
    _mCurtainHeight = self.frame.size.height - _mCurtainPaddingTop;
    
    _mTopImageHeight = self.frame.size.width*18/396;
    
    
}
 
 
#pragma mark drawRect 绘制图形
-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //将当前图形状态推入堆栈
    CGContextSaveGState(ctx);
    //*********绘制背景*********
    [self drawBackIamge];
    //*********绘制动态的进度条*********
    [self drawProgressBar:ctx];
    //*********绘制显示进度值文字*********
    [self drawProgressText:ctx];
    //*********绘制按钮图片*********
    [self drawImageButton:ctx];
    //*********绘制Top图片*********
    [self drawTopImage];
    
    //*********如果离线绘制离线背景遮挡层*********
    [self drawOfflineView:ctx];
    //把堆栈顶部的状态弹出
    CGContextRestoreGState(ctx);
    
}
 
/**
 如果离线绘制离线背景遮挡层
 
 @param ctx 画布
 */
-(void)drawOfflineView:(CGContextRef)ctx{
    if(_isOffline){
        CGRect drawRect2 = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        UIBezierPath *path3 = [UIBezierPath bezierPathWithRoundedRect:drawRect2 cornerRadius:5];
        [APP_DEFAULT_OFFLINE_COLOR set];
        CGContextAddPath(ctx, path3.CGPath);
        CGContextDrawPath(ctx, kCGPathFill);
    }
}
 
/**
 绘制背景图
 */
-(void)drawBackIamge{
    //绘制背景图
    CGRect rectBgImage = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [_bgImage drawInRect:rectBgImage];
}
 
/**
 绘制进度条
 
 @param ctx 画布
 */
-(void)drawProgressBar:(CGContextRef)ctx{
    
    //*******绘制剪裁限制显示区域*********
    CGRect drawRect2 = CGRectMake(_mCurtainStartX, _mCurtainPaddingTop, _mCurtainWidth, _mCurtainHeight);
    UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:drawRect2 cornerRadius:0];
    //    [APP_DEFAULT_BG set];
    CGContextAddPath(ctx, path2.CGPath);
    // 剪裁路径
    CGContextClip(ctx);
    
    _mCurtainX =  _mPercent * _mCurtainWidthHalf + _mCurtainPadding;
    
    //绘制按钮的图片
    
    if(_mPercent == 1){
        CGRect rectImage = CGRectMake(_mCurtainPadding, _mCurtainPaddingTop, _mCurtainWidth, _mCurtainHeight);
        [_curtainImage drawInRect:rectImage];
    }else{
        
        CGRect rectImage = CGRectMake(_mCurtainX - _mCurtainWidth , _mCurtainPaddingTop, _mCurtainWidth, _mCurtainHeight);
        [_curtainImage drawInRect:rectImage];
        
        
        CGRect rectImageRight = CGRectMake(self.frame.size.width - _mCurtainX, _mCurtainPaddingTop, _mCurtainWidth, _mCurtainHeight);
        [_curtainImage drawInRect:rectImageRight];
    }
    
}
 
 
 
/**
 绘制进度显示值文字
 
 @param ctx 画布
 */
-(void)drawProgressText:(CGContextRef)ctx{
    if(_isProgressTextShow){
        [self drawString:[NSString stringWithFormat:@"%d%@", _mProgress, _mProgressBarUnitSring] outsidePoint:_mCenterPoint];
    }
}
 
 
/**
绘制按钮的图片
 
 @param ctx 画布
 */
-(void)drawImageButton:(CGContextRef)ctx {
    CGContextRestoreGState(ctx);// 恢复到之前的context
    CGContextSaveGState(ctx);
    
    //绘制按钮的图片
    UIImage *image = [UIImage imageNamed:@"ic_wd_curtain_h_open"];
    CGRect rectImage = CGRectMake(_mCurtainX  - _mButtonWidth/2, self.frame.size.height/2 - _mButtonWidth/2, _mButtonWidth, _mButtonWidth);
    [image drawInRect:rectImage];
    
    
}
 
 
/**
 绘制Top的图片
 */
-(void)drawTopImage{
    CGRect rectImage = CGRectMake(0, 0, self.frame.size.width, _mTopImageHeight);
    [_bgTopImage drawInRect:rectImage];
    
}
 
 
 
/**
 drawString 现在当前进度值
 @param mText 显示文本
 @param outsidePoint 坐标
 */
- (void) drawString:(NSString *)mText outsidePoint:(CGPoint)outsidePoint{
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentCenter;
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:_mProgressTextSize],
                          NSForegroundColorAttributeName : _mProgressTextColor,
                          NSParagraphStyleAttributeName : paragraph
                          };
    
    //    CGRect textRect = CGRectMake(outsidePoint.x, outsidePoint.y, 80, 20);
    //    [mText drawInRect:textRect withAttributes:dic];
    
    //2019-08-15 修改文字绘制方法,最终实现文本居中效果
    CGSize textSize = [mText sizeWithAttributes:dic];
    CGPoint textPoint = CGPointMake(outsidePoint.x - textSize.width/2, outsidePoint.y - textSize.height/2 - _mButtonWidth);//根据中点坐标绘制
    [mText drawAtPoint:textPoint withAttributes:dic];
    
}
 
 
 
 
 
#pragma mark Touch Event 点击事件
-(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    [super beginTrackingWithTouch:touch withEvent:event];
    if(!_isClickable) return YES;//禁止点击
    _bTouchMove = NO;
    _mMoveCount = 0;
    
    //    CGPoint startPoint = [touch locationInView:self];
    //    _bIsInArcProgress = [self getStartPointIsInArcProgress:startPoint];//判断开始坐标是否在可点击区域
    CGPoint startPoint = [touch locationInView:self];
    bDownLeft =  (startPoint.x- self.frame.size.width/2) < 0;
    
    _bIsInCurtainProgress = YES;//不限制点击区域
    [self.mProgressChangedDelegate onStartTrackingTouch];
    return YES;
}
 
-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    [super endTrackingWithTouch:touch withEvent:event];
    if(!_isClickable) return;//禁止点击
    if(!_bTouchMove){//没移动,仅点击
        CGPoint lastPoint = [touch locationInView:self];
        [self getCurrentProgressWithLastPoint:lastPoint];
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
    [self.mProgressChangedDelegate onStopTrackingTouch:_mProgress];
}
 
 
-(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    [super continueTrackingWithTouch:touch withEvent:event];
    if(!_isClickable) return YES;//禁止点击
    if(_mMoveCount < 2){
        _mMoveCount++;
    }else{
        _bTouchMove = YES;  //开始移动
    }
    
    if(_bIsInCurtainProgress){//如果刚开始点击的坐标在可点击区域
        CGPoint lastPoint = [touch locationInView:self];
        [self getCurrentProgressWithLastPoint:lastPoint];
    }
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    return YES;
}
 
 
/**
 根据坐标计算出当前进度百分比和对应的进度值
 
 @param point 当前坐标
 */
-(void)getCurrentProgressWithLastPoint:(CGPoint)point {
    
    if(bDownLeft){
        float progressX = point.x - _mCurtainPadding;
        _mPercent = progressX / _mCurtainWidthHalf;
        
    }else{
        float progressX = point.x - self.frame.size.width/2;
        _mPercent = 1 - progressX / _mCurtainWidthHalf;
        
    }
    
    
    //        NSLog(@"point.y:%f   _mCurtainHeight:%f",point.x, _mCurtainWidthHalf);
    //        NSLog(@"_mPercent:%f",_mPercent);
    //
    if(_mPercent > 1){
        _mPercent = 1;
    }else if(_mPercent < 0){
        _mPercent = 0;
    }
    
    _mProgress = (1 -_mPercent) * (_mMaxValue - _mMinValue) + _mMinValue;
    
    if(_bTouchMove){
        [self.mProgressChangedDelegate onProgressChanged:_mProgress];
    }
    
    [self setNeedsDisplay];
}
 
 
/**
 根据进度值,计算当前 _mProgressAngle值
 
 @param mProgress 进度值
 */
-(void)setSeekBarProgressToValue:(int)mProgress{
    if(mProgress < _mMinValue){
        mProgress = _mMinValue;
    }
    if(mProgress > _mMaxValue){
        mProgress = _mMaxValue;
    }
    _mProgress = mProgress;
    _mPercent = 1 - (_mProgress - _mMinValue) / (_mMaxValue - _mMinValue);
    [self.mProgressChangedDelegate onProgressChanged:_mProgress];
}
 
#pragma mark 设置进度条位置
/**
 设置进度值
 
 @param mProgress 进度值
 */
-(void)setProgress:(int)mProgress{
    [self setSeekBarProgressToValue:mProgress];
    [self setNeedsDisplay];
}
 
 
/**
 设置进度显示值单位
 
 @param mString 单位字符
 */
-(void)setProgressBarUnitSring:(NSString *)mString{
    _mProgressBarUnitSring = mString;
}
 
 
/**
 设置最大值最小值
 
 @param mMinValue 最小值
 @param mMaxValue 最大值
 */
-(void)setMinAndMaxValue:(float)mMinValue mMaxValue:(float)mMaxValue{
    if(mMinValue < mMaxValue){
        _mMinValue = mMinValue;
        _mMaxValue = mMaxValue;
    }else{
        _mMinValue = mMaxValue;
        _mMaxValue = mMinValue;
    }
    _mProgress = _mMinValue;
    _mPercent = 0.0f;
}
 
 
/**
 重置布局
 
 @param mCGRect 布局
 
 */
-(void)initWithFrameSeekBar:(CGRect) mCGRect{
    self.frame = mCGRect;
    [self refreshFrame];
}
 
 
/**
 设置边距
 */
-(void)setCurtainPadding:(int)Padding{
    _CurtainPadding = Padding;
    [self onCurtainSizeChange];
}
 
 
/**
 设置顶部边距
 */
-(void)setCurtainPaddingTop:(int)Padding{
    _mCurtainPaddingTop = Padding;
    [self onCurtainSizeChange];
}
 
/**
 设置是否离线
 */
-(void)setOffline:(BOOL)isOffline{
    _isOffline = isOffline;
    _isClickable = !_isOffline;
    [self setNeedsDisplay];
}
 
/**
 获取坐标 X值
 */
-(CGFloat)getProgressX{
    return _mCurtainX;
}
 
@end