Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UILongPressGestureRecognitor未检测到背对背长按_Ios_Swift_Long Press_Uilongpressgesturerecogni - Fatal编程技术网

Ios UILongPressGestureRecognitor未检测到背对背长按

Ios UILongPressGestureRecognitor未检测到背对背长按,ios,swift,long-press,uilongpressgesturerecogni,Ios,Swift,Long Press,Uilongpressgesturerecogni,我创建了一个简单的Swift应用程序来测试UILongPressGestureRecognitor。该应用程序在屏幕上显示世界地图(MKMapView),用户可以长按地图,我只需打印出用户长按地图的次数 问题是,当我背对背长按地图时,只会检测到每一次交替长按!因此,如果我要打印出一个计数器变量,该变量在每次检测到长按时都会增加,那么我预计会出现以下情况: 我长按地图 显示“轻敲1次” 我等了几秒钟,然后再次长按地图 显示“轻敲2次” 我等了几秒钟,然后再次长按地图 显示“轻敲3次” 我得到的不是

我创建了一个简单的Swift应用程序来测试UILongPressGestureRecognitor。该应用程序在屏幕上显示世界地图(MKMapView),用户可以长按地图,我只需打印出用户长按地图的次数

问题是,当我背对背长按地图时,只会检测到每一次交替长按!因此,如果我要打印出一个计数器变量,该变量在每次检测到长按时都会增加,那么我预计会出现以下情况:

  • 我长按地图
  • 显示“轻敲1次”
  • 我等了几秒钟,然后再次长按地图
  • 显示“轻敲2次”
  • 我等了几秒钟,然后再次长按地图
  • 显示“轻敲3次”
  • 我得到的不是上述正确的功能,而是以下内容:

  • 我长按地图
  • 显示“轻敲1次”
  • 我等了几秒钟,然后再次长按地图
  • 什么都没发生
  • 我等了几秒钟,然后再次长按地图
  • 显示“轻敲2次”
  • 以下是我的简单代码:

    // Global variable
    var taps:Int = 0
    
    @IBAction func longPress(_ sender: UILongPressGestureRecognizer) {
    
      if sender.state == .began {
    
        // Increment counter and print to output
        taps += 1
        print("Tapped \(taps) times")
    
      }
    
    }
    
    上面的代码非常简单,所以我不确定出了什么问题

    另一个有趣的注意事项是,如果我在
    打印(“点击\(点击)次数”)
    之后添加下面的代码,它计算用户长时间按下的位置,并通过mapView.setRegion()方法移动到该位置,我将获得正确的功能

    let touchPositionOnScreen = sender.location(in: mapView)
    let convertedLatitude = mapView.convert(touchPositionOnScreen, toCoordinateFrom: mapView!).latitude
    let convertedLongitude = mapView.convert(touchPositionOnScreen, toCoordinateFrom: mapView!).longitude
    let locationCenter = CLLocationCoordinate2DMake(convertedLatitude, convertedLongitude)
    let currentSpan = mapView.region.span
    let moveToRegion = MKCoordinateRegion(center: locationCenter, span: currentSpan)
    mapView.setRegion(moveToRegion, animated: true)
    
    还有其他人观察到这种行为吗?请帮忙