![](/img/trans.png)
[英]How to set Zoom In and Zoom out in Google ios sdk map using Button
[英]how to zoom in/out map view using double taps+Swipe recognizer (like google maps) for ios
如何使用双击+滑动识别器(如Google地图)放大/缩小地图视图
什么是要添加到地图视图的识别器,我更喜欢swift lang。
谢谢,
预览:
@IBOutlet var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
map.showsUserLocation = true
map.delegate = self
let LongPress = UILongPressGestureRecognizer(target: self, action: "LongPress:")
LongPress.minimumPressDuration = 1
LongPress.numberOfTapsRequired = 2
map.addGestureRecognizer(LongPress)
我找到了解决方案。 显然,这是一个panGesture事件,但是您不能使用任何手势来实现,导致双击手势直到您离开屏幕时才会触发。 因此,另一种方法是重写mapView中的touchesMoved函数:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
for touch in touches {
if(touch.tapCount == 2) // tapCount 2 means tap on map twice
{
tapPositionY = touch.location(in: self).y // self is the mapView
}
}
}
tapPositionY用于存储用户在屏幕上触摸的位置。 这样我们就可以知道放大或缩小mapView了。
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
for touch in touches {
if(touch.tapCount == 2)
{
let positionY = touch.location(in: self).y
// 0.95 and 1.05 is zoom in/ zoom out parameter. you can change it
let zoom = positionY - tapPositionY > 0 ? 0.95 : 1.05
var region = self.region
var span = self.region.span
span.latitudeDelta = span.latitudeDelta * zoom
span.longitudeDelta = span.longitudeDelta * zoom
region.span = span
self.setRegion(region, animated: false)
tapPositionY = positionY // storage the position
}
}
}
setRegion动画必须为false,但效果相同。 如果设置为true,将像延迟一样。
对于双击,您可以使用以下代码:
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
view.addGestureRecognizer(tap)
func doubleTapped() {
// do your code here
}
在此链接上,您可以看到更多手势。
UIPinchGestureRecognizer提供了放大/缩小功能。 双击-UITapGestureRecognizer 。
除了双击之外,您不需要自己实现任何这些识别器。 MKMapView构建在与UIScrollView相同的类之上。 通过查看其协议,您将看到可用的方法。 但是,MKMapKit将覆盖它们,并且您不能将独立的委托分配给其内部的UIScrollView。 但是你可以覆盖
- mapView:regionWillChangeAnimated:
- mapView:regionDidChangeAnimated:
MKMapViewDelegate协议的方法,并手动比较可见矩形和缩放级别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.