SKCameraNode with Pan and Zoom

Using an SKCameraNode which works great for panning SKScene content larger than view size with Pan gesture. Also have a working constraint on the camera so it can't move beyond the scene content. Scene content can also be Zoomed in/out by scaling camera in a pinch gesture handler, great.


However if the the camera scaling is greater than 1 the camera shows the black space outside the scene content to fill the scaled content in view at the scene edge. This makes sense as the max distance to the edge the camera should be allowed is a function of zoom level but does anyone have any clever maths or patterns for this?


E.g. Does it mean redoing the constraint retangle every pan or is it best to transition the camera on pinch/zoom, is there a better way?


Any help appreciated.


dd

Post not yet marked as solved Up vote post of devondorset Down vote post of devondorset
2.4k views

Replies

I call my `updateCameraBounds` function each time my pinch gesture is triggered. Panning should always be fine as long as the constraint rectangle is accurate whenever a zoom event happens. Performance seems OK with this approach. Would be curious if there is anything more elegant, but this works for now.

thanks veeneck I ended up doing it this way but cant help thinking there could be a neater solution.

Would this be a good place to use KVO, watching the zoom value and updating the camera bounds based on its value?

In case this helps anyone...


Adding to my `updateCameraBounds` function each time my pinch gesture is triggered served me well (thanks veeneck) until I started scaling the camera with SKActions which as not related to gesture handling dicatates a more universal solution.


I looked at KVO (thanks deeeds) but is does not seem to play nice with spritekit, its simple to set up and half works (for pinch triggered changes to x/yScale etc.) but it doesnt fire for state changes during scaling SKAction's.


I've ended up putting my `updateCameraBounds` function in my SKScenes update method. This doesnt feel right somehow but it does tick all the boxes and no performance issues that I can detect.

@devondorset - Would you be willing to post your code as an example as I still can't get this to work properly?