Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么我的画在画了很长一段时间后会滞后?_Ios_Drawrect_Cgcontext_Cgpoint - Fatal编程技术网

Ios 为什么我的画在画了很长一段时间后会滞后?

Ios 为什么我的画在画了很长一段时间后会滞后?,ios,drawrect,cgcontext,cgpoint,Ios,Drawrect,Cgcontext,Cgpoint,我正在创建一个绘图应用程序。一开始我的画很流畅。当我画了很长一段时间的圆圈,我的画开始变得急躁。也许是因为数组不能处理太多的点 斯威夫特: override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { lastPoint = touches.first!.locationInView(self) self.setNeedsDisplay() } over

我正在创建一个绘图应用程序。一开始我的画很流畅。当我画了很长一段时间的圆圈,我的画开始变得急躁。也许是因为数组不能处理太多的点

斯威夫特:

   override func touchesBegan(touches: Set<UITouch>, withEvent       event: UIEvent?) {
    lastPoint = touches.first!.locationInView(self)

    self.setNeedsDisplay()
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    var newPoint = touches.first!.locationInView(self)

    lines.append(Line(start: lastPoint, end: newPoint))

    lastPoint = newPoint

    self.setNeedsDisplay()

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    var veryFirstPoint = touches.first!.locationInView(self)
    lines.append(Line(start: lastPoint, end:veryFirstPoint))
    self.setNeedsDisplay()
}


override func drawRect(rect: CGRect) {
    var context = UIGraphicsGetCurrentContext()
    CGContextBeginPath(context)

    for line in lines {

        CGContextMoveToPoint(context,line.start.x , line.start.y)
        CGContextAddLineToPoint(context, line.end.x, line.end.y)


    }
    CGContextSetRGBFillColor(context, 0, 0, 0, 1)

    CGContextSetLineWidth(context, 5)
    CGContextStrokePath(context)
    }
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
lastPoint=touchs.first!.locationInView(self)
self.setNeedsDisplay()
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
var newPoint=touchs.first!.locationInView(self)
行。追加(行(起点:lastPoint,终点:newPoint))
lastPoint=newPoint
self.setNeedsDisplay()
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
var veryFirstPoint=touchs.first!.locationInView(self)
行。追加(行(起始点:lastPoint,结束点:veryFirstPoint))
self.setNeedsDisplay()
}
重写func drawRect(rect:CGRect){
var context=UIGraphicsGetCurrentContext()
CGContextBeginPath(上下文)
排队{
CGContextMoveToPoint(上下文,line.start.x,line.start.y)
CGContextAddLineToPoint(上下文,line.end.x,line.end.y)
}
CGContextSetRGBFillColor(上下文,0,0,0,1)
CGContextSetLineWidth(上下文,5)
CGContextStrokePath(上下文)
}
在我的iPad mini 4上测试的示例: 左侧是绘制了一系列循环后的参差不齐的数字。右边是我画的前几个数字,它们很平滑


你是对的。每次您添加一条新的“线”,即每次触摸之后,CPU都会重新绘制许多路径。这会变得越来越处理器密集,因为您拥有的线路越多。一种解决方案是只重新绘制视图的“脏”部分。您可以使用
setNeedsDisplayInRect(rect:CGRect)
而不是
setNeedsDisplay()
来执行此操作。例如,您可以添加扩展名:

extension CGRect{
    static func rectWithTwoPoints(p1:CGPoint,p2:CGPoint) -> CGRect
    {
        return CGRectMake(min(p1.x, p2.x),min(p1.y, p2.y),fabs(p1.x - p2.x),fabs(p1.y - p2.y));
    }
}
这将给我一个包含任意两点的矩形。现在,在您的
touchsmoved:
touchsended:
中,我们可以调用
setNeedsDisplayInRect:
如下所示:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let newPoint = touches.first!.locationInView(self)

        lines.append(Line(start: lastPoint, end: newPoint))

        lastPoint = newPoint

        self.setNeedsDisplayInRect(CGRectInset(CGRect.rectWithTwoPoints((lines.last?.start)!, p2: (lines.last?.end)!),-10.0,-10.0)) //would be better to not force unwrap and use magic numbers, but you get the idea
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let veryFirstPoint = touches.first!.locationInView(self)
        lines.append(Line(start: lastPoint, end:veryFirstPoint))
        self.setNeedsDisplayInRect(CGRectInset(CGRect.rectWithTwoPoints((lines.last?.start)!, p2: (lines.last?.end)!),-10.0,-10.0))
    }
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让newPoint=touchs.first!.locationInView(self)
行。追加(行(起点:lastPoint,终点:newPoint))
lastPoint=newPoint
self.setNeedsDisplayInRect(CGRectInset(CGRect.rectWithTwoPoints((lines.last?.start)!,p2:(lines.last?.end)!,-10.0,-10.0))//最好不要强制展开并使用幻数,但你明白了
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
让veryFirstPoint=touchs.first!.locationInView(self)
行。追加(行(起始点:lastPoint,结束点:veryFirstPoint))
self.setNeedsDisplayInRect(CGRectInset(CGRect.RectWithTwo Points((lines.last?.start)!,p2:(lines.last?.end)!,-10.0,-10.0))
}

使用
CGRectInset
稍微扩展我们的rect,因为我们有一个路径宽度,如果我们只重新绘制rectwithpoints返回的rect,就会考虑到它。

这是可行的!唯一的问题发生在我画了一段时间后,当我开始画新的形状时,它会滞后一秒钟(只有当我从几个点开始时)。你知道这可能是什么吗?更新:我解决了这个问题。我所做的只是复制并粘贴self.setNeedsDisplayInRect在我的Touchs中开始并用lastPoint替换前两个参数。非常感谢你!