当绘图太多时,绘图应用程序滞后...我该如何解决这个问题?

时间:2015-10-20 18:46:46

标签: ios swift drawrect

当我在我的应用程序中绘制超过20秒时,绘图开始滞后并且变得非常慢。有没有办法可以解决这个问题,所以无论我在视图中绘制多少,它都不会滞后并且运行顺畅?这是我的代码:

import UIKit

class DrawView: UIView {

var lines: [Line] = []
var lastPoint: CGPoint!


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first {

        lastPoint = touch.locationInView(self)



    }

}


override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first {

        let newPoint = touch.locationInView(self)
        lines.append(Line(start: lastPoint, end: newPoint))
        lastPoint = newPoint


        self.setNeedsDisplay()


    }


}


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

    for line in lines {
        CGContextMoveToPoint(context, line.start.x, line.start.y)
        CGContextAddLineToPoint(context, line.end.x, line.end.y)

    }

    CGContextSetLineCap(context, CGLineCap.Round)
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1)
    CGContextSetLineWidth(context, 10.0)
    CGContextStrokePath(context)
    CGContextSetShouldSmoothFonts(context, true)
    CGContextSetLineJoin(context, CGLineJoin.Round)
    CGContextSetBlendMode(context, CGBlendMode.Normal)







}

0 个答案:

没有答案