在 Sprite 工具包中将文本写成一个圆圈 (swift)
Write a text in a circle in Sprite kit (swift)
我想画一个圆圈,然后在里面放一段文字。
我能做什么?
如果我移动圆圈或调整其大小,文本也会移动或调整大小
var color = UIColor(red: 0x00, green: 0x44, blue: 0x44, alpha: 1)
var Circle = SKShapeNode(circleOfRadius: 100 )
Circle.position = CGPointMake(frame.midX, frame.midY)
Circle.strokeColor = SKColor.blackColor()
Circle.glowWidth = 1.0
Circle.fillColor = color
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = "Hello, World!";
myLabel.fontSize = 60;
myLabel.position = CGPointMake(frame.midX, frame.midY)
myLabel.fontColor = UIColor.blackColor()
self.addChild(Circle)
更新:
使用您的完整示例。
不是 SpritKit 用户,但我相信每个节点都有一个 .addChild()
。
所以应该可以将它作为 child 添加到你的圈子中:
var color = UIColor(red: 0x00, green: 0x44, blue: 0x44, alpha: 1)
var Circle = SKShapeNode(circleOfRadius: 100 )
Circle.position = CGPointMake(frame.midX, frame.midY)
Circle.strokeColor = SKColor.blackColor()
Circle.glowWidth = 1.0
Circle.fillColor = color
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = "Hello, World!";
myLabel.fontSize = 60;
// Using half of the circle (center point)
myLabel.position = CGPointMake(Circle.frame.midX, Circle.frame.midY)
myLabel.fontColor = UIColor.blackColor()
Circle.addChild(myLabel)
self.addChild(Circle)
位置将相对于圆框。
同时拖动两者的一种方法是,您可以将两者添加到同一视图中,然后通过触摸事件更改两者的位置,例如在下面的代码中显示。
import SpriteKit
class GameScene: SKScene {
var deltaPoint = CGPointZero
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
var Circle = SKShapeNode(circleOfRadius: 100 )
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var color = UIColor(red: 0x00, green: 0x44, blue: 0x44, alpha: 1)
Circle.position = CGPointMake(frame.midX, frame.midY)
Circle.strokeColor = SKColor.blackColor()
Circle.glowWidth = 1.0
Circle.fillColor = color
myLabel.text = "Hello, World!";
myLabel.fontSize = 20
myLabel.position = CGPointMake(Circle.frame.midX, Circle.frame.midY)
myLabel.fontColor = UIColor.blackColor()
// Add them into same scene
self.addChild(Circle)
self.addChild(myLabel)
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let currentPoint = touch.locationInNode(self)
let previousPoint = touch.previousLocationInNode(self)
deltaPoint = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
deltaPoint = CGPointZero
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
deltaPoint = CGPointZero
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
var newPoint = CGPointMake(self.myLabel.position.x + self.deltaPoint.x, self.myLabel.position.y + self.deltaPoint.y)
// you can drag both item at same time
myLabel.position = newPoint
Circle.position = newPoint
deltaPoint = CGPointZero
}
}
您的标签字体大小应与您的圆圈大小成比例。
这是创建按钮的函数:
func createCircleButton(position: CGPoint, buttonSize: CGFloat, yourText: String) {
let circle = SKShapeNode( circleOfRadius: buttonSize)
circle.position = position
circle.fillColor = SKColor.blueColor()
let label = SKLabelNode(fontNamed:"ArialMT")
label.text = yourText
label.fontSize = circle.frame.size.height / 6;
label.fontColor = SKColor.whiteColor()
circle.addChild(label)
self.addChild(circle)
}
这样添加:
self.createCircleButton(CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2), buttonSize: self.frame.size.height / 5, yourText: "Your text")
Dharmesh Kheni 在 Swift 中的回答 3 + 将标签作为圆的子项并使标签居中对齐。
import SpriteKit
class GameScene: SKScene {
var deltaPoint = CGPoint.zero
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
var circle = SKShapeNode(circleOfRadius: 100 )
override func didMove(to view: SKView) {
let color = UIColor(red: 0x00, green: 0x44, blue: 0x44, alpha: 1)
circle.position = CGPoint(x: frame.midX, y: frame.midY)
circle.strokeColor = .black
circle.glowWidth = 1.0
circle.fillColor = color
myLabel.text = "Hello, World!";
myLabel.fontSize = 20
myLabel.horizontalAlignmentMode = .center
myLabel.verticalAlignmentMode = .center
myLabel.position = CGPoint(x:circle.frame.width/2, y: circle.frame.height/2)
myLabel.fontColor = .black
// Add them into same scene
self.addChild(circle)
circle.addChild(myLabel)
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let currentPoint = touch.location(in: self)
let previousPoint = touch.previousLocation(in: self)
deltaPoint = CGPoint(x: currentPoint.x - previousPoint.x,y: currentPoint.y - previousPoint.y)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
deltaPoint = CGPoint.zero
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
deltaPoint = CGPoint.zero
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
let newPoint = CGPoint(x: self.myLabel.position.x + self.deltaPoint.x,y: self.myLabel.position.y + self.deltaPoint.y)
// you can drag both item at same time
myLabel.position = newPoint
circle.position = newPoint
deltaPoint = CGPoint.zero
}
}
我想画一个圆圈,然后在里面放一段文字。 我能做什么?
如果我移动圆圈或调整其大小,文本也会移动或调整大小
var color = UIColor(red: 0x00, green: 0x44, blue: 0x44, alpha: 1)
var Circle = SKShapeNode(circleOfRadius: 100 )
Circle.position = CGPointMake(frame.midX, frame.midY)
Circle.strokeColor = SKColor.blackColor()
Circle.glowWidth = 1.0
Circle.fillColor = color
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = "Hello, World!";
myLabel.fontSize = 60;
myLabel.position = CGPointMake(frame.midX, frame.midY)
myLabel.fontColor = UIColor.blackColor()
self.addChild(Circle)
更新: 使用您的完整示例。
不是 SpritKit 用户,但我相信每个节点都有一个 .addChild()
。
所以应该可以将它作为 child 添加到你的圈子中:
var color = UIColor(red: 0x00, green: 0x44, blue: 0x44, alpha: 1)
var Circle = SKShapeNode(circleOfRadius: 100 )
Circle.position = CGPointMake(frame.midX, frame.midY)
Circle.strokeColor = SKColor.blackColor()
Circle.glowWidth = 1.0
Circle.fillColor = color
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = "Hello, World!";
myLabel.fontSize = 60;
// Using half of the circle (center point)
myLabel.position = CGPointMake(Circle.frame.midX, Circle.frame.midY)
myLabel.fontColor = UIColor.blackColor()
Circle.addChild(myLabel)
self.addChild(Circle)
位置将相对于圆框。
同时拖动两者的一种方法是,您可以将两者添加到同一视图中,然后通过触摸事件更改两者的位置,例如在下面的代码中显示。
import SpriteKit
class GameScene: SKScene {
var deltaPoint = CGPointZero
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
var Circle = SKShapeNode(circleOfRadius: 100 )
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var color = UIColor(red: 0x00, green: 0x44, blue: 0x44, alpha: 1)
Circle.position = CGPointMake(frame.midX, frame.midY)
Circle.strokeColor = SKColor.blackColor()
Circle.glowWidth = 1.0
Circle.fillColor = color
myLabel.text = "Hello, World!";
myLabel.fontSize = 20
myLabel.position = CGPointMake(Circle.frame.midX, Circle.frame.midY)
myLabel.fontColor = UIColor.blackColor()
// Add them into same scene
self.addChild(Circle)
self.addChild(myLabel)
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let currentPoint = touch.locationInNode(self)
let previousPoint = touch.previousLocationInNode(self)
deltaPoint = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
deltaPoint = CGPointZero
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
deltaPoint = CGPointZero
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
var newPoint = CGPointMake(self.myLabel.position.x + self.deltaPoint.x, self.myLabel.position.y + self.deltaPoint.y)
// you can drag both item at same time
myLabel.position = newPoint
Circle.position = newPoint
deltaPoint = CGPointZero
}
}
您的标签字体大小应与您的圆圈大小成比例。
这是创建按钮的函数:
func createCircleButton(position: CGPoint, buttonSize: CGFloat, yourText: String) {
let circle = SKShapeNode( circleOfRadius: buttonSize)
circle.position = position
circle.fillColor = SKColor.blueColor()
let label = SKLabelNode(fontNamed:"ArialMT")
label.text = yourText
label.fontSize = circle.frame.size.height / 6;
label.fontColor = SKColor.whiteColor()
circle.addChild(label)
self.addChild(circle)
}
这样添加:
self.createCircleButton(CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2), buttonSize: self.frame.size.height / 5, yourText: "Your text")
Dharmesh Kheni 在 Swift 中的回答 3 + 将标签作为圆的子项并使标签居中对齐。
import SpriteKit
class GameScene: SKScene {
var deltaPoint = CGPoint.zero
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
var circle = SKShapeNode(circleOfRadius: 100 )
override func didMove(to view: SKView) {
let color = UIColor(red: 0x00, green: 0x44, blue: 0x44, alpha: 1)
circle.position = CGPoint(x: frame.midX, y: frame.midY)
circle.strokeColor = .black
circle.glowWidth = 1.0
circle.fillColor = color
myLabel.text = "Hello, World!";
myLabel.fontSize = 20
myLabel.horizontalAlignmentMode = .center
myLabel.verticalAlignmentMode = .center
myLabel.position = CGPoint(x:circle.frame.width/2, y: circle.frame.height/2)
myLabel.fontColor = .black
// Add them into same scene
self.addChild(circle)
circle.addChild(myLabel)
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let currentPoint = touch.location(in: self)
let previousPoint = touch.previousLocation(in: self)
deltaPoint = CGPoint(x: currentPoint.x - previousPoint.x,y: currentPoint.y - previousPoint.y)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
deltaPoint = CGPoint.zero
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
deltaPoint = CGPoint.zero
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
let newPoint = CGPoint(x: self.myLabel.position.x + self.deltaPoint.x,y: self.myLabel.position.y + self.deltaPoint.y)
// you can drag both item at same time
myLabel.position = newPoint
circle.position = newPoint
deltaPoint = CGPoint.zero
}
}