swift - Limit the scaling of pinch gesture method to the height of screen -
i have 2 background images: background.png (1920 x 1280) , background@2x.png (3840 x 2560). also, app works on landscape mode.
at run time size of background image @ max.
problem: try print console height , of background image using pinch gesture method on each print, both values remain @ 4001.999.. x 2668.0, though, size of background image gets resize while pinching in , out.
what i'm trying accomplish is: access width , height of skspritenode containing background image can check if height equal of screen. if is, disables effect of pinch gesture of further zoom in. manage same effect zooming out.
if can suggest better algorithm doing this, please, feel free share. thanks!!
this 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 { 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 } } }
the answer discussed in comments question scale sprite node itself, rather parent sknode
. since sknode
not have visual content (see apple's discussion of sknode
's frame
property under "characteristics of nodes"), frame size (width: 0, height: 0)
. note should need bounding rectangle visual nodes within sknode
, calculateaccumulatedframe()
method available.
Comments
Post a Comment