我试图建立一个棋盘游戏,其中的电路板是建立精灵,你可以查看他们鸟瞰风格与SKCamera节点。我已经获得了平移,缩放和向下旋转,但他们并不觉得自然,因为我将手指放在屏幕上的任何位置都会围绕相机节点的中心旋转。Swift:SKIR中错误位置的UIRotationGesture中的手指中心点?
为了解决这个问题,我要创建一个SKNode并将这两者之间的手指作为相机的旋转和缩放锚在哪里。
但是我不能让手指的中心点的位置与在gamescene权COORDS排队。
在哪里,我曾经把我的手指在整个比赛场面,它认为它旋转,中心应该是永远在那个红色的一滴。
的SKView放置在故事板作为SpriteKit视图,并绑定到屏幕的与顶部和底部填充的左和右边缘:
内部的ViewController的:
@IBOutlet weak var Gameview: SKView!
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene(size:Gameview.frame.size)
Gameview.presentScene(scene)
}
内的GameScene.swift,let a = sender.location(in: self.view)
似乎是问题所在。
class GameScene: SKScene, UIGestureRecognizerDelegate {
var newCamera: SKCameraNode!
let rotateRec = UIRotationGestureRecognizer()
let zoomRec = UIPinchGestureRecognizer()
//let panRec = UIPanGestureRecognizer()
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer)
-> Bool {return true}
override func didMove(to view: SKView) {
rotateRec.addTarget(self, action: #selector(GameScene.rotatedView (_:)))
rotateRec.delegate = self
self.view!.addGestureRecognizer(rotateRec)
zoomRec.addTarget(self, action: #selector(GameScene.zoomedView (_:)))
zoomRec.delegate = self
self.view!.addGestureRecognizer(zoomRec)
//panRec.addTarget(self, action: #selector(GameScene.pannedView (_:)))
//self.view!.addGestureRecognizer(panRec)
newCamera = SKCameraNode()
newCamera.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(newCamera)
self.camera = newCamera
//drawBoard()
//I removed this function as it had nothing to do with the question
}
@objc func rotatedView(_ sender:UIRotationGestureRecognizer) {
print(sender.location(in: self.view))
let square = SKSpriteNode(color: #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1), size: CGSize(width: 10, height: 10))
let a = sender.location(in: self.view)
let b = -a.x
let c = -a.y
let d = CGPoint(x: b, y: c)
square.position = d
self.addChild(square)
newCamera.zRotation += sender.rotation
sender.rotation = 0
if sender.state == .ended {
let rotateAmount = abs(Double(newCamera.zRotation) * Double(180)/Double.pi)
let remainder = abs(rotateAmount.truncatingRemainder(dividingBy: 90))
//print(rotateAmount)
//print(remainder)
if remainder < 10 {
newCamera.zRotation = CGFloat((rotateAmount-remainder)*Double.pi/180)
} else if remainder > 80 {
newCamera.zRotation = CGFloat((rotateAmount-remainder+90)*Double.pi/180)
}
}
}
@objc func zoomedView(_ sender:UIPinchGestureRecognizer) {
let pinch = SKAction.scale(by: 1/sender.scale, duration: 0.0)
newCamera.run(pinch)
sender.scale = 1.0
}
//@objc func pannedView(_ sender:UIPanGestureRecognizer) {
// sender.translation(in:)
//}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let previousLocation = touch.previousLocation(in: self)
let deltaY = location.y - previousLocation.y
let deltax = location.x - previousLocation.x
newCamera!.position.y -= deltaY
newCamera!.position.x -= deltax
}
}
}
我没有删除函数drawboard(),因为它在问题的上下文中是多余的。