Reputation:
Below is my method for zooming. I am trying to implement it, so that when the user uses one finger to zoom (move finger up or down in specified region), it should zoom straight down or up just like normal (see snapchat as an example).
The problem is the code I have written changes latitude at the same time, which is unintended.
Why is this happening and how can I change it?
@objc func panGesture(_ sender: UIPanGestureRecognizer) {
print("recognized?")
// note that 'view' here is the overall video preview
let velocity = sender.velocity(in: mapView) //view
//print(sender.translation(in: view), "<-- waht is val?")
// print(sender.setTranslation(CGPoint(x: 16, y: 495), in: view), "<-- mmmmm")
if velocity.y >= 2 || velocity.y <= 2 {
let minimumZoomFactor: CGFloat = 1.0
let maximumZoomFactor: CGFloat = 17.0 // artificially set a max useable zoom of 14x (maybe shuld be icncrease?)
// clamp a zoom factor between minimumZoom and maximumZoom
func clampZoomFactor(_ factor: CGFloat) -> CGFloat {
return min(max(factor, minimumZoomFactor), maximumZoomFactor)
}
func update(scale factor: CGFloat) {
mapView.zoomLevel = Double(exactly: factor)! //maaybe setZoomLevel(... and animate it bit by bit?
}
var lastVal = 0
//BELOW IS SENDER.STATE THINGS!!!
switch sender.state {
case .began:
originalZoomLevel = mapView.zoomLevel//device.videoZoomFactor
//print(originalZoomLevel, "<-- what is initialZoom11111111???")
case .changed:
// distance in points for the full zoom range (e.g. min to max), could be view.frame.height
let fullRangeDistancePoints: CGFloat = 300.0 //dont know fi this is right??
// extract current distance travelled, from gesture start
let currentYTranslation: CGFloat = sender.translation(in: view).y
// calculate a normalized zoom factor between [-1,1], where up is positive (ie zooming in)
let normalizedZoomFactor = -1 * max(-1,min(1,currentYTranslation / fullRangeDistancePoints))
// calculate effective zoom scale to use
let newZoomFactor = clampZoomFactor(CGFloat(originalZoomLevel) + normalizedZoomFactor /** (maximumZoomFactor - minimumZoomFactor)*/)
print(originalZoomLevel, "<-- what is initialZoom???")
print(newZoomFactor, "<-- what is newZoomFactor???")
// update device's zoom factor'
update(scale: newZoomFactor)
print(lastVal - Int(mapView.centerCoordinate.latitude), " : The change is here")
lastVal = Int(mapView.centerCoordinate.latitude)
print(mapView.centerCoordinate, " : Cenetr courdenate in .changed")
case .ended, .cancelled:
print(originalZoomLevel, "<-- what is this???")
break
default:
break
}
}
}
Upvotes: 3
Views: 394
Reputation:
The problem in your code is in your func update(scale ...)
Replace this code:
mapView.zoomLevel = Double(exactly: factor)!
With this:
mapView.setCenter(centerCoordOrig, zoomLevel: Double(exactly: factor)!, animated: false)
By the way, create a new variable outside for the centerCoordOrig
.
Upvotes: 1