Super_DL_csdn 2016-12-22 07:39 采纳率: 0%
浏览 2469

iOS CAShapeLayer和UIBezierPath无限绘制图线如何不卡

现在公司有一个要求:连接蓝牙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);
});
  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2016-12-31 02:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站