Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – 在没有framedrops的情况下创建无限的cgpath大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我需要不断创建一个cgpath.目前我这样做: func createLine(){ var rand = randombetweennumbers(1, 2) currentY-- if rand < 1.5{ currentX-- CGPathAddLineToPoint(leftPath, nil
@H_675_13@
我需要不断创建一个cgpath.目前我这样做:
func createLine(){
        var rand = randombetweennumbers(1,2)
        currentY--
        if rand < 1.5{
            currentX--
            CGPathAddLineToPoint(leftPath,nil,currentX,currentY)
        }else{
            currentX++
            CGPathAddLineToPoint(leftPath,currentY)
        }
        CGPathAddLineToPoint(rightPath,currentX+tileSize,currentY)
        lineNode.path = leftPath
        rightNode.path = rightPath

}

并称之为:

NSTimer.scheduledTimerWithTimeInterval(0.05,target: self,SELEctor: SELEctor("startTile"),userInfo: nil,repeats: truE)

但问题是,帧随着时间的推移越来越低.有什么我必须改变,以便帧率不再下降?

我的目标是创建一个随机的无尽路径.

在绘制逐渐增加的行数的同时保持高FPS计数的关键是快速达到向场景添加更多行对帧速率影响很小或没有影响的状态.至少有两种方法可以实现这一目标.

两者中最直接的方法是定期将先前绘制的线转换为SKTexture,并将结果显示为SKSpriteNode的纹理.以下是步骤:

>创建一个用作行容器的SKNode
>创建一个将用作线条画布的SKSpriteNode
>创建一个用于绘制新线条的SKShapeNode
>将容器添加到场景,将画布和形状节点添加到容器
>使用形状节点的path属性绘制一组连接的线段
>当行数达到预定值时,将容器的内容转换为’SKTexture’
>将画布的纹理属性设置为SKTexture.请注意,由于画布也是容器的子项,因此其内容也将添加到纹理中
>泡沫,冲洗,重复步骤5 – 7

这是Swift中的一个示例实现,它在iPhone 6设备上以60 FPS绘制无穷无尽的行(您应该在不使用模拟器的设备上测试性能):

class GameScene: SKScene {
    // 1. Create container to hold new and old lines
    var lineContainer = SKNode()
    // 2. Create canvas
    var lineCanvas:SKSpriteNode?
    // 3. Create shape to draw new lines
    var lineNode = SKShapeNode()

    var lastDrawTime:Int64 = 0
    var lineCount = 0
    var timeScan:Int64 = 0
    var path = CGPathCreateMutable()

    var lastPoint = CGPointZero

    override func didMoveToView(view:SKView) {
        scaleMode = .ResizeFill

        // 4. Add the container to the scene and the canvas to the container 
        addChild(lineContainer)
        lineCanvas = SKSpriteNode(color:SKColor.clearColor(),size:view.frame.sizE)
        lineCanvas!.anchorPoint = CGPointZero
        lineCanvas!.position = CGPointZero
        lineContainer.addChild(lineCanvas!)
        lastPoint = CGPointMake(view.frame.size.width/2.0,view.frame.size.height/2.0)
    }

    // Returns a random value in the specified range
    func randomInRange(minValue:CGFloat,maxValue:CGFloat) -> CGFloat {
        let r = CGFloat(Double(arc4random_uniform(UInt32.maX))/Double(UInt32.maX))
        return (maxValue-min@R_489_7548@ * r + minValue
    }

    func drawLine() {
        if (CGPathIsEmpty(path)) {
            // Create a new line that starts where the prevIoUs line ended
            CGPathMoveToPoint(path,lastPoint.x,lastPoint.y)
            lineNode.path = nil
            lineNode.lineWidth = 1.0
            lineNode.strokeColor = SKColor.blueColor()
            lineNode.zPosition = 100
            lineContainer.addChild(lineNodE)
        }
        // Add a random line segment
        let x = randomInRange(size.width*0.1,maxValue: size.width*0.9)
        let y = randomInRange(size.height*0.1,maxValue: size.height*0.9)
        CGPathAddLineToPoint(path,x,y)
        lineNode.path = path
        // Save the current point so we can connect the next line to the end of the last line
        lastPoint = CGPointMake(x,y)
    }

    override func update(currentTime: CFTimeInterval) {
        let lineDrawTime = timeScan / 10
        // 5. Draw a new line every 10 updates. Increment line count
        if (lineDrawTime != lastDrawTimE) {
            drawLine()
            ++lineCount
        }
        // 6. and 7. Add all newly and prevIoUsly drawn lines to the canvas
        if (lineCount == 8) {
            addLinesToTexture()
            lineCount = 0
        }
        lastDrawTime = lineDrawTime
        ++timeScan
    }

    func addLinesToTexture () {
        // Convert the contents of the line container to an SKTexture
        let texture = self.view!.textureFromNode(lineContainer)
        // Display the texture
        lineCanvas!.texture = texture
        // Start a new line
        lineNode.removeFromParent()
        path = CGPathCreateMutable()
    }
}

大佬总结

以上是大佬教程为你收集整理的swift – 在没有framedrops的情况下创建无限的cgpath全部内容,希望文章能够帮你解决swift – 在没有framedrops的情况下创建无限的cgpath所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。