Skip to content

Commit

Permalink
Added zooming
Browse files Browse the repository at this point in the history
- Added a starting scale var to keep track of the scale when the zooming starts
- Added a new pinch gesture recognizer for the zoom
- Added the method handlePinch for the zoom logic
  • Loading branch information
Lawrence-Tran committed May 12, 2018
1 parent c600c72 commit 9080505
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions Source/CTPanoramaView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ import ImageIO
return tan(self.yFov/2 * .pi / 180.0) * 2 * self.radius
}()

private var startScale = 0.0

private var xFov: CGFloat {
return yFov * self.bounds.width / self.bounds.height
}
Expand Down Expand Up @@ -145,7 +147,7 @@ import ImageIO
add(view: sceneView)

scene.rootNode.addChildNode(cameraNode)
yFov = 70
yFov = 80

sceneView.scene = scene
sceneView.backgroundColor = UIColor.black
Expand Down Expand Up @@ -199,8 +201,11 @@ import ImageIO
sceneView.gestureRecognizers?.removeAll()

if method == .touch {
let panGestureRec = UIPanGestureRecognizer(target: self, action: #selector(handlePan(panRec:)))
sceneView.addGestureRecognizer(panGestureRec)
let panGestureRec = UIPanGestureRecognizer(target: self, action: #selector(handlePan(panRec:)))
sceneView.addGestureRecognizer(panGestureRec)

let pinchRec = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(pinchRec:)))
sceneView.addGestureRecognizer(pinchRec)

if motionManager.isDeviceMotionActive {
motionManager.stopDeviceMotionUpdates()
Expand Down Expand Up @@ -277,6 +282,25 @@ import ImageIO
reportMovement(CGFloat(-cameraNode.eulerAngles.y), xFov.toRadians())
}
}

@objc func handlePinch(pinchRec: UIPinchGestureRecognizer) {
if pinchRec.numberOfTouches != 2 {
return
}

let zoom = Double(pinchRec.scale)
switch pinchRec.state {
case .began:
startScale = cameraNode.camera!.yFov
case .changed:
let fov = startScale / zoom
if fov > 20 && fov < 80 {
cameraNode.camera!.yFov = fov
}
default:
break
}
}

public override func layoutSubviews() {
super.layoutSubviews()
Expand Down

0 comments on commit 9080505

Please sign in to comment.