现在公司有一个要求:连接蓝牙BLE4.0之后,获取传递过来的数据,然后以点的形式连接成线。
我采用的是CAShapeLayer和UIBezierPath绘制曲线,当一开始点比较少的时候,可以非常流畅的把线画出来
但是当点的数目达到3000左右,就会开始卡顿。
因为我们的数据是每秒25个数据,在scrollView上显示的时候回非常卡顿,请问有什么好办法来解决这个问题呢????
以下是绘制的代码:
UIBezierPath *path = [[UIBezierPath alloc] init];
path.usesEvenOddFillRule = YES;
NSString * tt=nil;
float height=self.topScrollView.frame.size.height;
if (objc.BLE_View.minusRangeValue==0) {
tt=[NSString stringWithFormat:@"{%.2f,%.2f}",timer_number*1.2,objc.BLE_View.positive_k*height*(1-[string floatValue]/[objc.BLE_View.positiveRangeValue floatValue])];
}
else
{
if (isPositive) {
tt=[NSString stringWithFormat:@"{%.2f,%.2f}",timer_number*1.2,objc.BLE_View.positive_k*height*(1-[string floatValue]/[objc.BLE_View.positiveRangeValue floatValue])];
}
else
{
tt=[NSString stringWithFormat:@"{%.2f,%.2f}",timer_number*1.2,objc.BLE_View.minus_k*height*(1+[string floatValue]/[objc.BLE_View.minusRangeValue floatValue])];
}
}
if (!pointLast.x) {
pointLast=CGPointFromString(tt);
[path moveToPoint:pointLast];
[path addLineToPoint:pointLast];
}
else
{
[path moveToPoint:pointLast];
[path addLineToPoint:pointLast];
[path addLineToPoint:CGPointFromString(tt)];
}
//create shape layer
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.drawsAsynchronously=YES;
shapeLayer.strokeColor = [UIColor greenColor].CGColor;
shapeLayer.fillColor = [UIColor whiteColor].CGColor;
shapeLayer.lineWidth = 3;
shapeLayer.lineJoin = kCALineJoinRound;//终点处理
shapeLayer.lineCap = kCALineCapRound;//线条拐角
shapeLayer.path = path.CGPath;
//add it to our view
[shapeLayerArr addObject:shapeLayer];
dispatch_sync(dispatch_get_main_queue(), ^{
[_topScrollView.layer addSublayer:shapeLayer];
// 设置layer的animation
_topScrollView.layer.shouldRasterize = YES;
NSLog(@"------》%f,-------》%f",pointLast.x-10,pointLast.y-10);
[_topScrollView setNeedsDisplayInRect:CGRectMake(pointLast.x-10, pointLast.y-10, 40, 40)];
[_topScrollView.layer setNeedsDisplayInRect:CGRectMake(pointLast.x-10, pointLast.y-10, 40, 40)];
if (pointLast.x>_topScrollView.contentSize.width) {
_topScrollView.contentSize=CGSizeMake(_topScrollView.contentSize.width+_topScrollView.frame.size.width, 0);
_topScrollView.contentOffset=CGPointMake(pointLast.x, 0);
}
pointLast=CGPointFromString(tt);
});