Ios 在Sprite kit(swift)中的圆圈中书写文本
我想画一个圆圈,在里面放一段文字。 我能做什么 如果我移动或调整了圆的大小,文本也会移动或调整大小Ios 在Sprite kit(swift)中的圆圈中书写文本,ios,swift,sprite-kit,skshapenode,Ios,Swift,Sprite Kit,Skshapenode,我想画一个圆圈,在里面放一段文字。 我能做什么 如果我移动或调整了圆的大小,文本也会移动或调整大小 var color = UIColor(red: 0x00, green: 0x44, blue: 0x44, alpha: 1) var Circle = SKShapeNode(circleOfRadius: 100 ) Circle.position = CGPointMake(frame.midX, frame.midY) C
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()
。
因此,应该可以将其作为子对象添加到您的圆圈中:
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
}
}
导入SpriteKit
类游戏场景:SKScene{
var deltaPoint=CGPointZero
设myLabel=SKLabelNode(FontName:“粉笔掸子”)
var圆圈=SKShapeNode(圆圈半径:100)
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
var color=UIColor(红色:0x00,绿色:0x44,蓝色:0x44,alpha:1)
Circle.position=CGPointMake(frame.midX,frame.midY)
Circle.strokeColor=SKColor.blackColor()
圆圈宽度=1.0
Circle.fillColor=颜色
myLabel.text=“你好,世界!”;
myLabel.fontSize=20
myLabel.position=CGPointMake(Circle.frame.midX,Circle.frame.midY)
myLabel.fontColor=UIColor.blackColor()
//将它们添加到同一场景中
self.addChild(圆)
self.addChild(myLabel)
}
覆盖功能触摸移动(触摸:设置,withEvent事件:UIEvent){
如果让触摸=触摸。首先作为?UITouch{
让currentPoint=touch.locationInNode(自)
让previousPoint=触摸。previousLocationInNode(自身)
deltaPoint=CGPointMake(currentPoint.x-previousPoint.x,currentPoint.y-previousPoint.y)
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent){
deltaPoint=CGPointZero
}
覆盖func touchesCancelled(touchs:Set!,withEvent:UIEvent!){
deltaPoint=CGPointZero
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
var newPoint=CGPointMake(self.myLabel.position.x+self.deltaPoint.x,self.myLabel.position.y+self.deltaPoint.y)
//您可以同时拖动这两个项目
myLabel.position=新点
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
}
}
导入SpriteKit
类游戏场景:SKScene{
var deltaPoint=CGPoint.zero
设myLabel=SKLabelNode(FontName:“粉笔掸子”)
var圆圈=SKShapeNode(圆圈半径:100)
覆盖func didMove(到视图:SKView){
设color=UIColor(红色:0x00,绿色:0x44,蓝色:0x44,alpha:1)
circle.position=CGPoint(x:frame.midX,y:frame.midY)
circle.strokeColor=.black
圆圈宽度=1.0
circle.fillColor=颜色
myLabel.text=“你好,世界!”;
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
//将它们添加到同一场景中
self.addChild(圆)
circle.addChild(myLabel)
}
覆盖功能触摸移动(触摸:设置,withEvent事件:UIEvent){
如果让触摸=触摸。首先作为?UITouch{
让currentPoint=触摸位置(in:self)
让previousPoint=touch.previousLocation(in:self)
deltaPoint=CGPoint(x:currentPoint.x-previousPoint.x,y:currentPoint.y-previousPoint.y)
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent){
deltaPoint=CGPoint.zero
}
覆盖func touchesCancelled(touchs:Set!,withEvent:UIEvent!){
deltaPoint=CGPoint.zero
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
设newPoint=CGPoint(x:self.myLabel.position.x+self.deltaPoint.x,y:self.myLabel.position.y+self.deltaPoint.y)
//您可以同时拖动这两个项目
myLabel.position=新点
circle.position=newPoint
deltaPoint=CGPoint.zero
}
}
要将我的标签
显示到圆圈中吗?是的。我可以将圆圈和标签移动到一起,你需要更清楚你的目标。是否希望文本围绕圆的外侧弯曲?你想让它保持垂直,但要换行以填充空间吗?我想把文本写成一个圆圈,而文本是圆圈的一部分。对于移动和动作,我只使用circle。对不起,我的英语不是很好,我希望文本不要跳出圆圈。如果文本比圆圈大,我想修剪它(遮罩它)myLabel.position=CGPointMake(frame.midX,frame.midY)
但它会从圆圈的上/左角将屏幕缩小一半。覆盖函数中的错误触摸移动(触摸:设置,带事件:UIEvent){>>使用未声明的类型“设置”。这是什么错误?您可以在此处找到旧xcode的方法: