如果背景和进度条共享相同的尺寸,则以下代码有效。如果进度条较短,则只要有更新,条形就会跳转一下 - 就像缩放进度条会更改进度条的原点一样。
在Swift和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)
}
答案 0 :(得分:0)
检查以下内容: