Wil*_*ick 5 ios sprite-kit skspritenode swift swift4.1
如何用一根手指“触摸并拖动”旋转 SpriteNode,以便:
我尝试过的事情:
使用 CGAffineTransform 的 CGAffineTransformMakeRotation来影响 SpriteKit 中旋钮的旋转。但我无法弄清楚如何在 SpriteNode 上使用 CGAffineTransformMakeRotation。我可以将不同类型的对象放入我的场景中或在它上面,但这是不对的。
例如,Matthijs Hollemans 的 MHRotaryKnob https://github.com/hollance/MHRotaryKnob
。我将 Hollemans 旋钮从 Objective C 转换为 Swift 4,但在尝试在 SpriteKit 中使用它来旋转精灵时遇到了麻烦。我不明白,因为我不知道如何knobImageView.transform = CGAffineTransformMakeRotation (newAngle * M_PI/180.0);在 Swift 中使用 SpriteKit。我知道我可以使用 Hollemans Objective C 类并将 UIImage 推送到我的场景顶部,但这似乎不是最好也最优雅的解决方案。
我还将Wex的解决方案从 Objective C 翻译成 Swift Rotate image on center using a finger touch Using Allan Weir的关于处理代码中 CGAffineTransform 部分的建议/sf/answers/2920685281/ 但是那不起作用。
我试过直接在我的精灵上设置zRotation而不使用 .physicalBody 无济于事。它具有相同的生涩运动,不会在您希望它停止的地方停止。并以手指拖动的相反方向移动 - 即使您将“-”放在弧度角前面。
我也试过0x141E堆栈溢出的解决方案: 将旋转节点围绕一个固定的点 这是下面贴使用.sks文件的解决方案(有点改进释放我已经试过未修饰的版本,它是没有更好的)。这个解决方案摇摆不定,不能顺利地跟随我的手指,不能始终如一地将旋钮移动到特定点。如果我设置physicsBody 属性来创建摩擦、质量或angularDamping 和linearDamping 或降低SKSpriteNode 的速度都没有关系。
我也在互联网上搜索使用 SpriteKit 在 Swift 4 中寻找一个好的解决方案,但到目前为止无济于事。
import Foundation
import SpriteKit
class Knob: SKSpriteNode
{
var startingAngle: CGFloat?
var currentAngle: CGFloat?
var startingTime: TimeInterval?
var startingTouchPoint: CGPoint?
override init(texture: SKTexture?, color: UIColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)
self.setupKnob()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupKnob()
}
func setupKnob() {
self.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(self.size.height))
self.physicsBody?.pinned = true
self.physicsBody?.isDynamic = true
self.physicsBody?.affectedByGravity = false
self.physicsBody?.allowsRotation = true
self.physicsBody?.mass = 30.0
//self.physicsBody?.friction = 0.8
//self.physicsBody?.angularDamping = 0.8
//self.physicsBody?.linearDamping = 0.9
//self.speed = 0.1
self.isUserInteractionEnabled = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in:self)
let node = atPoint(location)
startingTouchPoint = CGPoint(x: location.x, y: location.y)
if node.name == "knobHandle" {
let dx = location.x - node.position.x
let dy = location.y - node.position.y
startingAngle = atan2(dy, dx)
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in:self)
let node = atPoint(location)
guard startingAngle != nil else {return}
if node.name == "knobHandle" {
let dx:CGFloat = location.x - node.position.x
let dy:CGFloat = location.y - node.position.y
var angle: CGFloat = atan2(dy, dx)
angle = ((angle) * (180.0 / CGFloat.pi))
let rotate = SKAction.rotate(toAngle: angle, duration: 2.0, shortestUnitArc: false)
self.run(rotate)
startingAngle = angle
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
var touch: UITouch = touches.first!
var location: CGPoint = touch.location(in: self)
self.removeAllActions()
startingAngle = nil
startingTime = nil
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果我删除到度数的转换并将 SKAction 的持续时间更改为 1.0,SKAction.rotate(toAngle:duration: 1.0, shortestUnitArc:)那么它几乎可以工作:不是生涩,但仍然是生涩;杠杆不会改变方向 - 这意味着有时如果您尝试将其移动到与它行进的方向相反的方向,它会继续围绕锚点沿旧方向移动,而不是您拖动它的新方向。
编辑 2:GreatBigBore和我讨论了 SKAction 旋转和 self.zRotation——上面的代码和下面的代码。
编辑 3:sicvayne为SKScene建议了一些代码,我已经适应了 SKSpriteNode(如下)。它不会始终如一地移动,也不会让您停在特定的地方。
import Foundation
import SpriteKit
class Knob: SKSpriteNode {
var fingerLocation = CGPoint()
override init(texture: SKTexture?, color: UIColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)
self.setupKnob()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupKnob()
}
func setupKnob() {
self.isUserInteractionEnabled = true
}
func rotateKnob(){
let radians = atan2(fingerLocation.x - self.position.x, fingerLocation.y - self.position.y)
self.zRotation = -radians
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
fingerLocation = touch.location(in: self)
}
self.rotateKnob()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
/*override func update(_ currentTime: TimeInterval) { //this is a SKScene function
rotateKnob()
}*/
}
Run Code Online (Sandbox Code Playgroud)
这看起来如何快速:
if point.x.sign == .minus {
angle = atan(point.y/point.x) + CGFloat.pi/2
} else {
angle = atan(point.y/point.x) + CGFloat.pi/2 + CGFloat.pi
}
Run Code Online (Sandbox Code Playgroud)
此外,您还必须获取场景中另一个对象的坐标,因为整个坐标系随该对象旋转:
let body = parent?.childNode(withName: "objectInScene")
let point = touch.location(in: body!)
Run Code Online (Sandbox Code Playgroud)