gpt4 book ai didi

Swift:UIRotationGesture 中手指的中心点在 SKView 中的错误位置?

转载 作者:行者123 更新时间:2023-11-30 12:08:53 25 4
gpt4 key购买 nike

我正在尝试构建一个棋盘游戏,其中棋盘由 Sprite 构建,您可以使用 SKCamera 节点以鸟瞰方式查看它们。我已经实现了平移、缩放和向下旋转,但它们感觉不自然,因为无论我将手指放在屏幕上,它都会围绕相机节点的中心旋转。

为了解决这个问题,我打算创建一个 SKNode 并将其放置在手指之间,作为相机旋转和缩放的 anchor 。

但是我无法使手指中心点的位置与游戏场景中的正确坐标对齐。

我创建了一些红色框来调试和查找我的问题,如下所示: Gamescene screenshot

无论我将手指放在整个游戏场景中的哪个位置来旋转它,它认为应该在的中心始终位于那个红色 Blob 中。

SKView 作为 SpriteKit View 放置在 Storyboard 上,并通过顶部和底部填充绑定(bind)到屏幕的左右边缘:

View Controller 内部:

    @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(),因为它在问题的上下文中是多余的。

最佳答案

您需要使用页面中显示的函数将坐标从 sender.location 转换为 skview 中的坐标:

https://developer.apple.com/documentation/spritekit/skscene

--> https://developer.apple.com/documentation/spritekit/skscene/1520395-convertpoint

在rotatedview()函数中,输入代码:

let bluesquare = SKSpriteNode(color: #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1), size: CGSize(width: 10, height: 10))

let a = sender.location(in: self.view)
bluesquare.position = self.convertPoint(fromView: a)

self.addChild(bluesquare)

...将在两根手指的中心位置添加蓝色 Sprite 。

关于Swift:UIRotationGesture 中手指的中心点在 SKView 中的错误位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46325680/

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