Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class GameScene: SKScene, UIGestureRecognizerDelegate {
- var SceneCamera = SKCameraNode()
- var LastLocation = CGPoint()
- @objc func handlePanFrom(recognizer: UIPanGestureRecognizer) {
- var translation = recognizer.translation(in: recognizer.view!)
- translation = CGPoint(x: translation.x + LastLocation.x, y: -translation.y + LastLocation.y)
- camera?.position = CGPoint(x: position.x - translation.x, y: position.y - translation.y)
- if recognizer.state == .ended {
- LastLocation = translation
- }
- }
- @objc func handlePinchFrom(recognizer: UIPinchGestureRecognizer) {
- var anchorPoint = recognizer.location(in: recognizer.view)
- anchorPoint = self.convertPoint(fromView: anchorPoint)
- let anchorPointInCamera = camera?.convert(anchorPoint, from: self)
- camera?.setScale((camera?.xScale)! / (recognizer.scale))
- let CameraAnchorPointInScene = self.convert(anchorPointInCamera!, from: camera!)
- let TranslationOfAnchorInScene = (x: anchorPoint.x - CameraAnchorPointInScene.x, y: anchorPoint.y - CameraAnchorPointInScene.y)
- camera?.position = CGPoint(x: (camera?.position.x)! + TranslationOfAnchorInScene.x, y: (camera?.position.y)! + TranslationOfAnchorInScene.y)
- recognizer.scale = 1.0
- setCameraConstraints()
- }
- override func didMove(to view: SKView) {
- // Setup camera control
- SceneCamera.position = CGPoint(x: (scene?.frame.midX)!, y: (scene?.frame.midY)!)
- let playableAreaScaleForIphoneAndIpadPro = (view.bounds.size.height) / (scene?.frame.height)!
- SceneCamera.setScale(1/playableAreaScaleForIphoneAndIpadPro)
- self.addChild(SceneCamera)
- self.camera = SceneCamera
- setCameraConstraints()
- // Camera Control: Pinch(Zoom)
- let PinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(handlePinchFrom(recognizer:)))
- PinchGesture.delegate = self
- self.view?.addGestureRecognizer(PinchGesture)
- let PanGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanFrom(recognizer:)))
- PanGesture.minimumNumberOfTouches = 2
- PanGesture.delegate = self
- self.view?.addGestureRecognizer(PanGesture)
- }
- func setCameraConstraints() {
- guard let camera = camera else { return }
- let scaledSize = CGSize(width: size.width * camera.xScale, height: size.height * camera.yScale)
- let boardContentRect = scene?.frame
- let xInset = min((scaledSize.width / 2) - 0.0, (boardContentRect?.width)! / 2)
- let yInset = min((scaledSize.height / 2) - 0.0, (boardContentRect?.height)! / 2)
- let insetContentRect = boardContentRect?.insetBy(dx: xInset, dy: yInset)
- let xRange = SKRange(lowerLimit: (insetContentRect?.minX)!, upperLimit: (insetContentRect?.maxX)!)
- let yRange = SKRange(lowerLimit: (insetContentRect?.minY)!, upperLimit: (insetContentRect?.maxY)!)
- let levelEdgeConstraint = SKConstraint.positionX(xRange, y: yRange)
- levelEdgeConstraint.referenceNode = scene
- camera.constraints = [levelEdgeConstraint]
- }
- func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
- return true
- }
- }
Add Comment
Please, Sign In to add comment