CALayer drawinContext名为@ 60fps,但查看更新图形@ 1fps

时间:2015-10-14 06:43:12

标签: ios macos cocoa core-animation quartz-graphics

我正在尝试使用Cocoa和Quartz框架在OSX中使用NSBezierPath实现图形绘制视图,并在我去的时候添加/删除数据点。

在drawRect中这样做很好,因为图表经常更新,但是当我需要增加总数据点/采样率时遇到了性能问题。

我决定转到drawLayer:inContext:但是当函数以60fps调用时,视图在调用函数时不更新图形,而是以1fps更新。

我在这里做错了什么?

class CustomDrawLayer: CALayer {


convenience init(view: NSView, drawsAsynchronously : Bool = false) {
    self.init()
    self.bounds = view.bounds
    self.anchorPoint = CGPointZero
    self.opaque = false
    self.frame = view.frame
    self.drawsAsynchronously = drawsAsynchronously

    //        for multiple draws in hosting view
    //        self.delegate = self

}

override func actionForLayer(layer: CALayer, forKey event: String) -> CAAction? {
    return nil
}}

override func drawLayer(layer: CALayer, inContext ctx: CGContext) {

    if layer == self.layer {
        Swift.print("axes drawing")
        graphBounds.origin = self.frame.origin
        graphAxes.drawAxesInRect(graphBounds, axeOrigin: plotOrigin, xPointsToShow: CGFloat(totalSecondsToDisplay), yPointsToShow: CGFloat(totalChannelsToDisplay))
    }

    if layer == self.board {
        Swift.print(1/NSDate().timeIntervalSinceDate(fpsTimer))
        fpsTimer = NSDate()
        drawPointsInGraph(graphAxes, context: ctx)

    }
}

    func drawPointsInGraph(axes: AxesDrawer, context:  CGContext)
    {
            color.set()

            var x : CGFloat = 0
            var y : CGFloat = 0

            for var channel = 0; channel < Int(totalChannelsToDisplay); channel++ {

                path.removeAllPoints()

                var visibleIndex = (dirtyRect.origin.x - axes.position.x) / (axes.pointsPerUnit.x / samplingRate)
                if visibleIndex < 2 {
                    visibleIndex = 2
                }
                for var counter = Int(visibleIndex); counter < dataStream![channel].count; counter++ {

                    if dataStream![channel][counter] == 0  {
                        if path.elementCount > 0 {
                            path.stroke()
                        }

                        break
                    }


                    let position = axes.position
                    let ppY = axes.pointsPerUnit.y
                    let ppX = axes.pointsPerUnit.x

                    let channelYLocation = CGFloat(channel)

                    x = position.x + CGFloat(counter-1) * (ppX / samplingRate)
                    y = ((channelYLocation * ppY) + position.y) + (dataStream![channel][counter-1] * (ppY))
                    path.moveToPoint(CGPoint(x: align(x), y: align(y)))

                    x = position.x + CGFloat(counter) * (ppX / samplingRate)
                    y = ((channelYLocation * ppY) + position.y) + (dataStream![channel][counter] * (ppY) )


                    path.lineToPoint(CGPoint(x: align(x), y: align(y)))




                    if x > (axes.position.x + axes.bounds.width) * 0.9 {

                        graphAxes.forwardStep = 5
                        dirtyRect = graphBounds

                        for var c = 0; c < Int(totalChannelsToDisplay); c++ {
                            for var i = 0; i < Int(samplingRate) * graphAxes.forwardStep; i++
                            {
                                dataStream![c][i] = 0
                            }

                        }

                        return
                    }


                }



                path.stroke()

        }

        if inLiveResize {
            dirtyRect = graphBounds
        } else {
            dirtyRect.origin.x = x
            dirtyRect.origin.y = bounds.minY
            dirtyRect.size.width = 10
            dirtyRect.size.height = bounds.height
        }


    }

2 个答案:

答案 0 :(得分:1)

如果需要频繁绘制路径,请查看CAShapeLayer,您可以在其中更改路径属性。这将是硬件加速,并且比drawRect或drawLayer快得多。

答案 1 :(得分:1)

您应该以60 Hz的频率调用功能,这是非常罕见的。在任何情况下,您都不应该尝试以60 Hz的频率调用绘图功能;在Cocoa中从未有意义。如果你真的是指在屏幕刷新间隔,&#34;&#34;请参阅CADisplayLink,它专门用于允许您在屏幕刷新间隔绘制。这可能比60 Hz慢。如果您尝试精确绘制60 Hz,则可能会失去同步并导致动画中出现节拍。但这真的只适用于实时视频之类的东西。如果那就是你所拥有的,那么这就是工具,但它听起来并不像它。

理解您的代码有点困难。我们不清楚你的60fps在哪里。但我假设你正在尝试做的是动画绘制图形。如果是这样,正如Mark F所说,请参阅CAShapeLayer。它内置了自动路径动画,绝对是你想要的。它会自动处理时间并与屏幕刷新和GPU优化同步,以及许多其他不应该尝试解决的问题。

即使CAShapeLayer不是您想要的,您也应该关注核心动画,它旨在与您一起制作动画值并根据需要重绘。例如,它会自动处理在多个核心上渲染您的图层,这将显着提高性能。有关详情,请参阅Animating Custom Layer Properties