gpt4 book ai didi

swift - 无法在 Swift 中的场景中心放置标签

转载 作者:行者123 更新时间:2023-11-30 14:05:16 25 4
gpt4 key购买 nike

我正在尝试将 UILabel 放置在场景的正中心。我通过获取中心值然后使用convertPointToView函数来做到这一点,然后减去宽度,结果不在中心。如果有人能告诉我为什么我所做的事情是错误的,或者甚至可以为我解决它,我将非常感激。结果如下:( http://i1087.photobucket.com/albums/j461/niiooo/Screen%20Shot%202015-09-10%20at%205.26.37%20PM.png )

import SpriteKit

class GameScene: SKScene {

var testSprite: SKSpriteNode = SKSpriteNode(imageNamed: "Spaceship")
var testLabel: UILabel = UILabel()

func defineTestLabel(var #label: UILabel) -> UILabel {
var labelXPos: Int = Int()

labelXPos = Int(convertPointToView(CGPoint(x: (self.size.width / 2) - 50, y: 0)).x)
label = UILabel(frame: CGRect(x: labelXPos, y: 100, width: 100, height: 100))
label.text = "Test"
label.textAlignment = NSTextAlignment.Center
label.layer.borderColor = UIColor.blackColor().CGColor
label.layer.borderWidth = 5
return label
}

override func didMoveToView(view: SKView) {
self.view?.addSubview(defineTestLabel(label:self.testLabel))
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */

for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
}
}

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}

最佳答案

您可以使用场景 midX 减去标签宽度的一半。

关于swift - 无法在 Swift 中的场景中心放置标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32511888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com