由于工作需要,做一個(gè)拍照顯示的進(jìn)度條思劳;樣式鋪滿整個(gè)屏幕
自定義view:
h文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TakePhotoProgress : UIView
@property(assign,nonatomic)CGFloat progress;
@end
NS_ASSUME_NONNULL_END
m文件
#import "TakePhotoProgress.h"
@interface TakePhotoProgress()
@property (nonatomic, strong) UILabel *progressLabel;
@end
@implementation TakePhotoProgress
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.progressLabel = ({
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/2 - 100, self.frame.size.height/2 - 25, 200, 50)];
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:15];
label.textAlignment = NSTextAlignmentCenter;
label;
});
[self addSubview:self.progressLabel];
}
return self;
}
- (void)drawRect:(CGRect)rect {
// 定義扇形中心
CGPoint origin = CGPointMake(kScreenWidth/2 +50, kScreenheight/2);
// 定義扇形半徑
CGFloat radius = kScreenWidth/2 +100;
// 設(shè)定扇形起點(diǎn)位置
CGFloat startAngle = - M_PI_2;
// 根據(jù)進(jìn)度計(jì)算扇形結(jié)束位置
CGFloat endAngle = startAngle + self.progress * M_PI * 2;
// 根據(jù)起始點(diǎn)、原點(diǎn)、半徑繪制弧線
UIBezierPath *sectorPath = [UIBezierPath bezierPathWithArcCenter:origin radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
// 從弧線結(jié)束為止繪制一條線段到圓心态辛。這樣系統(tǒng)會(huì)自動(dòng)閉合圖形,繪制一條從圓心到弧線起點(diǎn)的線段挺尿。
[sectorPath addLineToPoint:origin];
// 設(shè)置扇形的填充顏色
[RGBCOLOR(0, 0, 0, 0.5) set];
// 設(shè)置扇形的填充模式
[sectorPath fill];
}
//重寫progress的set方法奏黑,可以在賦值的同時(shí)給label賦值
- (void)setProgress:(CGFloat)progress{
_progress = progress;
// 對label進(jìn)行賦值
// self.progressLabel.text = [NSString stringWithFormat:@"圖片接收中%0.2f%%",progress * 100];
self.progressLabel.text = [NSString stringWithFormat:@"%@ %0.2f%%",NSLocalizedString(@"圖片接收中:", nil),progress * 100];
[self setNeedsDisplay];
}
@end
接下來就是調(diào)用了
在需要調(diào)用的方法直接引用
image.png
-(void)takePhotoProgress{
if(!_PhotoProgress){
_PhotoProgress = [[TakePhotoProgress alloc] initWithFrame:CGRectMake(-50, 0, kScreenWidth +100, kScreenheight)];
[self. view addSubview:_PhotoProgress];
[self startTimer];
}
}
定時(shí)器
#pragma mark - 定時(shí)器
- (NSTimer *)Ttimer
{
if (_Ttimer == nil) {
_Ttimer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(progressChange) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_Ttimer forMode:NSRunLoopCommonModes]; //防止計(jì)時(shí)器卡頓等其它現(xiàn)象
}
return _Ttimer;
}
- (void)startTimer
{
self. takeprogress = 0.0;
[self. Ttimer setFireDate:[NSDate distantPast]]; //開啟定時(shí)器
}
-(void)stopTimer{
[_Ttimer invalidate]; //銷毀
_Ttimer=nil;
}
- (void)progressChange
{
self. takeprogress += 0.025;
NSLog(@"1 progress = %f", self.takeprogress);
if (self. takeprogress >= 1) {
[self stopTimer];
if(_PhotoProgress){
[_PhotoProgress removeFromSuperview];
_PhotoProgress =nil;
}
return;
}
_PhotoProgress. progress = self.takeprogress;
}
歡迎評(píng)論區(qū)交流炊邦。