jjzjj

ios - 带有 MKMapKit 的 Swift GestureRecognizers - 放下图钉并拖动

coder 2023-09-04 原文

我正在尝试将多个 UIGestureReconizers 与一个 MKMapView 结合使用,用户可以将图钉放在上面并四处拖动。它有一个 callout。我在 TabbarControllerNavigationController 中实现它。我目前有:

1) PanGestureRecognizer 将 Tabbar 和 Navigation 项设置为动画离开屏幕。这在不干扰平移 map 的情况下工作正常。

2) 设置为一次点击的 TapGestureRecognizer 使两个项目从 1) 动画回到屏幕上。

3) TapGestureRecognizer 设置为两次点击允许底层 MKMapView 缩放功能起作用。此 GestureRecognizer 的 delegate 具有 gestureRecognizer。shouldRecognizeSimultaneouslyWithGestureRecognizer 设置为 true

这些在 viewDidLoad 中设置如下:

 // This sets up the pan gesture recognizer to hide the bars from the UI.
    let panRec: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: "didDragMap:")
    panRec.delegate = self
    mapView.addGestureRecognizer(panRec)

    // This sets up the tap gesture recognizer to un-hide the bars from the UI.
    let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "didTapMap:")
    singleTap.delegate = self
    singleTap.numberOfTapsRequired = 1
    singleTap.numberOfTouchesRequired = 1
    mapView.addGestureRecognizer(singleTap)

    // This sets up the double tap gesture recognizer to enable the zoom facility.
    // In order to pass double-taps to the underlying `MKMapView` the delegate for this recognizer (self) needs to return true from
    // gestureRecognizer.shouldRecognizeSimultaneouslyWithGestureRecognizer
    let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer()
    doubleTap.numberOfTapsRequired = 2
    doubleTap.numberOfTouchesRequired = 1
    doubleTap.delegate = self
    mapView.addGestureRecognizer(doubleTap)

// This delays the single-tap recognizer slightly and ensures that it will NOT fire if there is a double-tap
    singleTap.requireGestureRecognizerToFail(doubleTap)

当我尝试实现 UILongPressGestureRecognizer 以允许将图钉放到 map 上时,我的问题就出现了。我正在尝试使用添加到 viewDidLoad 的以下内容:

// This sets up the long tap to drop the pin.
    let longTap: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "didLongTapMap:")
    longTap.delegate = self
    longTap.numberOfTapsRequired = 0
    longTap.minimumPressDuration = 0.5
    mapView.addGestureRecognizer(longTap)

这是我的操作方法:

func didLongTapMap(gestureRecognizer: UIGestureRecognizer) {
    // Get the spot that was tapped.
    let tapPoint: CGPoint = gestureRecognizer.locationInView(mapView)
    let touchMapCoordinate: CLLocationCoordinate2D = mapView.convertPoint(tapPoint, toCoordinateFromView: mapView)

    var viewAtBottomOfHierarchy: UIView = mapView.hitTest(tapPoint, withEvent: nil)
    if let viewAtBottom = viewAtBottomOfHierarchy as? MKPinAnnotationView {
        return
    } else {
        if .Began == gestureRecognizer.state {
            // Delete any existing annotations.
            if mapView.annotations.count != 0 {
                mapView.removeAnnotations(mapView.annotations)
            }

            annotation = MKPointAnnotation()
            annotation.coordinate = touchMapCoordinate

            mapView.addAnnotation(annotation)
            _isPinOnMap = true

            findAddressFromCoordinate(annotation.coordinate)
            updateLabels()
        }
    }
}

这确实允许在长按时放下图钉,单击一次将显示标注,但如果拖动启动不够快,则第二次点击以按住并拖动会导致第二个图钉掉落。第二个 pin 掉落到前一个 pin 悬停的空间中,用户可以拖动,但新的 pin 掉落很尴尬且看起来不对。

我正在尝试使用这条线:

if let viewAtBottom = viewAtBottomOfHierarchy as? MKPinAnnotationView {

将水龙头返回到 MKMapView 并防止另一个 pin 被删除,但返回永远不会被调用,即使此行上的断点显示 viewAtBottom 的类型为 MapKit.MKPinAnnotationView。我哪里出错了?

最佳答案

如果我理解正确的话,我想我可能会找到你问题的答案。 当放下一个图钉然后在不放置另一个图钉的情况下四处拖动屏幕时遇到问题,对吗? 这是我的代码,我一直在做类似的事情,这似乎对我有用。 导入 UIKit 导入 MapKit 导入核心位置

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet var map: MKMapView!

    var manager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(gestureRecognizer:)))

        uilpgr.minimumPressDuration = 2

        map.addGestureRecognizer(uilpgr)

        if activePlace == -1 {

            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyBest
            manager.requestWhenInUseAuthorization()
            manager.startUpdatingLocation()
            self.map.showsUserLocation = true

        } else {

            //GET PLACE DETAILS TO DISPLAY ON MAP

            if places.count > activePlace {

                if let name = places[activePlace]["name"]{

                    if let lat = places[activePlace]["lat"]{

                        if let lon = places[activePlace]["lon"]{

                            if let latitude = Double(lat) {

                                if let longitude = Double(lon) {

                                    let span = MKCoordinateSpan(latitudeDelta:  0.05, longitudeDelta: 0.05)

                                    let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

                                    let region = MKCoordinateRegionMake(coordinate, span)

                                    self.map.setRegion(region, animated: true)

                                    let annotation = MKPointAnnotation()

                                    annotation.coordinate = coordinate

                                    annotation.title = name

                                    self.map.addAnnotation(annotation)

                                }

                            }

                        }

                    }

                }

            }

        }


    }


    func longpress(gestureRecognizer: UIGestureRecognizer) {

        if gestureRecognizer.state == UIGestureRecognizerState.began {

            let touchPoint = gestureRecognizer.location(in: self.map)

            let newCoordinate = self.map.convert(touchPoint, toCoordinateFrom: self.map)

            let location = CLLocation(latitude: newCoordinate.latitude, longitude: newCoordinate.longitude)

            var title = ""

            CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) in

                if error != nil {

                    print(error)

                } else {

                    if let placemark = placemarks?[0] {

                        if placemark.subThoroughfare != nil {

                            title += placemark.subThoroughfare! + " "

                        }

                        if placemark.thoroughfare != nil {

                            title += placemark.thoroughfare! + " "

                        }



                    }

                }

                if title == "" {

                    title = "Added \(NSDate())"

                }

                let annotation = MKPointAnnotation()

                annotation.coordinate = newCoordinate

                annotation.title = title

                self.map.addAnnotation(annotation)

                places.append(["name": title, "lat":String(newCoordinate.latitude), "lon":String(newCoordinate.longitude)])

                UserDefaults.standard.set(places, forKey: "places")

            })

        }

    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

            let location = CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude)

            let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)

            let region = MKCoordinateRegion(center: location, span: span)

            self.map.setRegion(region, animated: true)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

希望对您有所帮助,问题也可能是您的最短长按持续时间仅为 0.5。

关于ios - 带有 MKMapKit 的 Swift GestureRecognizers - 放下图钉并拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24777919/

有关ios - 带有 MKMapKit 的 Swift GestureRecognizers - 放下图钉并拖动的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. ruby-on-rails - 带有 Zeus 的 RSpec 3.1,我应该在 spec_helper 中要求 'rspec/rails' 吗? - 2

    使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做

  5. Ruby:如何使用带有散列的 'send' 方法调用方法? - 2

    假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而

  6. ruby-on-rails - 带有 Pry 的 Rails 控制台 - 2

    当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question

  7. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  8. 带有 attr_accessor 的类上的 Ruby instance_eval - 2

    我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到

  9. ruby-on-rails - Rails 渲染带有驼峰命名法的 json 对象 - 2

    我在一个简单的RailsAPI中有以下Controller代码:classApi::V1::AccountsControllerehead:not_foundendendend问题在于,生成的json具有以下格式:{id:2,name:'Simpleaccount',cash_flows:[{id:1,amount:34.3,description:'simpledescription'},{id:2,amount:1.12,description:'otherdescription'}]}我需要我生成的json是camelCase('cashFlows'而不是'cash_flows'

  10. ruby-on-rails - 在 Ruby 或 Rails 中,hash.merge({ :order => 'asc' }) can return a new hash with a new key. 什么可以返回带有已删除键的新散列? - 2

    在Ruby(或Rails)中,我们可以做到new_params=params.merge({:order=>'asc'})现在new_params是一个带有添加键:order的散列。但是是否有一行可以返回带有已删除key的散列?线路new_params=params.delete(:order)不会工作,因为delete方法返回值,仅此而已。我们必须分3步完成吗?tmp_params=paramstmp_params.delete(:order)returntmp_params有没有更好的方法?因为我想做一个new_params=(params[:order].blank?||para

随机推荐