Reputation: 53
I have two background images: background.png (1920 x 1280) and [email protected] (3840 x 2560). Also, app works only on landscape mode.
At run time the size of my background image is at its max.
PROBLEM: I try to print to the console the height and with of my background image using the pinch gesture method but on each print, both values remain at 4001.999.. x 2668.0, even though, the size of my background image does gets resize while pinching in and out.
What I'm trying to accomplish is: Access the width and height of my SKSpriteNode containing my background image so that I can check if its height is equal to that of the screen. If it is, it disables the effect of the pinch gesture of further zoom in. I would also like to manage the same effect but for zooming out.
If someone can suggest a better algorithm for doing this, please, feel free to share. Thanks!!
This is my code:
// 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
}
}
}
Upvotes: 1
Views: 654
Reputation: 548
The answer discussed in the comments to the question is to scale the sprite node itself, rather than its parent SKNode
. Since the SKNode
does not have any visual content (see Apple's discussion of the SKNode
's frame
property under "Characteristics of Nodes"), its frame size will be (width: 0, height: 0)
. Note that should you need the bounding rectangle for all visual nodes within an SKNode
, the calculateAccumulatedFrame()
method is available.
Upvotes: 1