swift – 在没有framedrops的情况下创建无限的cgpath

swift – 在没有framedrops的情况下创建无限的cgpath,第1张

概述我需要不断创建一个cgpath.目前我这样做: func createLine(){ var rand = randomBetweenNumbers(1, 2) currentY-- if rand < 1.5{ currentX-- CGPathAddLineToPoint(leftPath, nil 我需要不断创建一个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-minValue) * 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所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1035695.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存