Swift: Progress bar jumps when using showing progress

↘锁芯ラ 提交于 2019-12-25 02:57:16

问题


The code below works if the background and the progress bar share the same dimensions. If the progress bar is shorter, the bar jumps around a bit whenever there is an update -- as if scaling the progress bar changes the origin for the progress bar.

What's the right way to implement custom progress bars in Swift and SpriteKit?

private func initProgressLayer(gridLeftX: CGFloat, gridWidth: CGFloat) {
    // Set progress bar width
    var progressWidth = gridWidth
    let barWidth = CGFloat(50)
    let barHeight = ProgressBarHeight

    // Set layer position
    let posY = statsLayer.position.y - CGFloat(ProgressBarHeight/2)
    let posX = CGFloat(0.0) - CGFloat(progressWidth/2)
    progressLayer.position = CGPoint(x: posX, y: posY)

    // Create background for progress bar
    let backgroundRect = CGRect(x: 0, y: 0, width: progressWidth, height: ProgressBarHeight)
    let background = SKShapeNode(rect: backgroundRect)
    background.fillColor = ProgressBackgroundColor

    // Create progress bar
    let barRect = CGRect(x: 10, y: 0, width: barWidth, height: barHeight)
    progressBar = SKShapeNode(rect: barRect)
    progressBar.fillColor = ProgressBarColor

    // Add background and progress bar to layer
    progressLayer.addChild(background)
    progressLayer.addChild(progressBar)
}


private func updateProgress(var progress: CGFloat) {
    if (progress > 1.0) {
        progress = 1.0
    }
    let scaleAction = SKAction.scaleXTo(progress, duration: NSTimeInterval(0.3))
    scaleAction.timingMode = .EaseIn
    progressBar.runAction(scaleAction)
}

回答1:


Check for the following:

  • is updateProgress: being called from a background thread? (it shouldn't)
  • how often is updateProgress: called? if often enough you might want to remove the scaleX animation and apply the scale directly to the sprite. In fact, you might want to try doing that anyway, it's probably what might be causing you issues.
  • Have you shifted the anchorPoint of the progress bar sprite? This will create random behaviours when you try and edit frame properties.


来源:https://stackoverflow.com/questions/30178554/swift-progress-bar-jumps-when-using-showing-progress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!