废话不多说直接上代码,简单明了。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,weak) CAShapeLayer *layer;
@property (nonatomic,weak) CAShapeLayer *layer2;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,strong) UIBezierPath *path;
@property (nonatomic,strong) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.dataArr = [NSMutableArray arrayWithArray:@[@0.01,@0.01,@0.01,@0.01,@0.01,@0.01,@0.01,@0.01]];
[self startMeterTimer];
}
#pragma mark- displayLink
- (void)startMeterTimer {
[self stopTimer];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerRefresh) userInfo:nil repeats:YES];
}
- (void)stopTimer {
[self.timer invalidate];
}
- (void)timerRefresh {
CGFloatheight = (arc4random() %10+1)/10.0;
[self.dataArr removeLastObject];
[self.dataArr insertObject:@(height) atIndex:0];
[self creatLayer];
[self creatLayer2];
}
- (void)creatLayer {
self.path= [UIBezierPathbezierPath];
CGFloatheight =CGRectGetHeight(self.layer.frame);
for(inti =0; i <self.dataArr.count; i++) {
CGFloatx =100- (i *5+5);
CGFloatH = [self.dataArr[i]floatValue] * height;
CGFloatbeginY = height /2.0- H /2.0;
CGFloatendY = height /2.0+ H /2.0;
[_pathmoveToPoint:CGPointMake(x, beginY)];
[_path addLineToPoint:CGPointMake(x, endY)];
}
self.layer.path=_path.CGPath;
}
- (CAShapeLayer *)layer {
if(_layer==nil) {
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame=CGRectMake(100,200,100,20);
layer.strokeColor= [UIColorredColor].CGColor;
layer.lineWidth=3;
[self.view.layeraddSublayer:layer];
_layer= layer;
}
return_layer;
}
- (void)creatLayer2 {
self.path= [UIBezierPathbezierPath];
CGFloatheight =CGRectGetHeight(self.layer2.frame);
for(inti =0; i <self.dataArr.count; i++) {
CGFloatx = (i *5+5);
CGFloatH = [self.dataArr[i]floatValue] * height;
CGFloatbeginY = height /2.0- H /2.0;
CGFloatendY = height /2.0+ H /2.0;
[_pathmoveToPoint:CGPointMake(x, beginY)];
[_path addLineToPoint:CGPointMake(x, endY)];
}
self.layer2.path=_path.CGPath;
}
- (CAShapeLayer *)layer2 {
if(_layer2==nil) {
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame=CGRectMake(190,200,100,20);
layer.strokeColor= [UIColorredColor].CGColor;
layer.lineWidth=3;
[self.view.layeraddSublayer:layer];
_layer2= layer;
}
return _layer2;
}
@end
网友评论