我有两个背景图片:background.png (1920 x 1280) 和 background@2x.png (3840 x 2560)。此外,应用程序仅适用于横向模式。
在运行时,我的背景图像的大小是最大的。
问题:我尝试使用捏合手势方法向控制台打印我的背景图像的高度和宽度,但在每次打印时,两个值都保持在 4001.999.. x 2668.0,尽管我的背景图像的大小确实如此在捏合和捏合时调整大小。
我想要完成的是:访问包含我的背景图像的 SKSpriteNode 的宽度和高度,以便我可以检查它的高度是否等于屏幕的高度。如果是,它将禁用进一步放大的捏合手势的效果。我也想管理相同的效果,但要缩小。
如果有人可以建议更好的算法,请随时分享。谢谢!!
这是我的代码:
// Background
backgroundNode = createBackground()
self.addChild(backgroundNode)
println("anchorPoint = \(self.anchorPoint)")
}
override func didMoveToView(view: SKView) {
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action: Selector("pinchView:"))
pinchRecognizer.delegate = self
view.addGestureRecognizer(pinchRecognizer)
}
// NODES
func createBackground() -> SKNode {
let backgroundNode = SKNode()
let node = SKSpriteNode(imageNamed: "background/background")
node.name = "bg"
node.setScale(scaleFactor)
backgroundNode.addChild(node)
return backgroundNode
}
// GESTURES
func pinchView(sender: UIPinchGestureRecognizer) {
let minScale: CGFloat = 0.15
let maxScale: CGFloat = 3
let tempScale = backgroundNode.xScale * sender.scale
let pinch: SKAction = SKAction.scaleBy(sender.scale, duration: 0.0)
if tempScale > minScale && tempScale < maxScale {
backgroundNode.runAction(pinch)
sender.scale = 1.0
}
var width = backgroundNode.childNodeWithName("bg")?.self.frame.size.width
var height = backgroundNode.childNodeWithName("bg")?.self.frame.size.height
println("width: \(width)") // 4001.999...
println("height: \(height)") // 2668.0
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if touches.count == 1 {
for touch: AnyObject in touches {
let prevLoc: CGPoint = touch.previousLocationInView(view)
let curentLoc: CGPoint = touch.locationInView(view)
let deltaX: CGFloat = curentLoc.x - prevLoc.x
let deltaY: CGFloat = curentLoc.y - prevLoc.y
backgroundNode.position.x += deltaX
backgroundNode.position.y -= deltaY
}
}
}
最佳答案
问题评论中讨论的答案是缩放 sprite 节点本身,而不是其父 SKNode
。由于 SKNode
没有任何可视内容(请参阅“节点特征”下 SKNode
的 frame
属性的 Apple's discussion),它的帧大小将是 (width: 0, height: 0)
。请注意,如果您需要 SKNode
中所有可视节点的边界矩形,则可以使用 calculateAccumulatedFrame()
方法。
关于swift - 将捏合手势方法的缩放限制为屏幕的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30536579/