ios - 类型 'UIGestureRecognizer' 的值没有成员 'numberOfTapsRequired' 为什么会出现此错误?

标签 ios swift uigesturerecognizer

我在添加双击时收到此错误“'UIGestureRecognizer' 类型的值在我的代码中没有成员'numberOfTapsRequired',我阅读了许多关于该主题的其他帖子,但我找不到问题。你能发现我在 addDoubleTap 函数中做错了什么吗?这是完整的代码

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController, MKMapViewDelegate, UIGestureRecognizerDelegate {

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var dropPinButton: UIButton!
    @IBOutlet weak var centerMApButton: UIButton!

    var pin: AnnotationPinn!
    // variables that hold values for selected icon, to be used for displaying the pin
    var dataReceived: String? 

    // udemy location manager couse
    var locationManager = CLLocationManager()
    let authorizationStatus = CLLocationManager.authorizationStatus()
    let regionRadius: Double = 1000.0



    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self
        locationManager.delegate = self

        configureLocationServices()
        setCoordinates()
//        addDoubleTap()
        let latitude: Double = setCoordinates().lat    //44.498955
        let longitude: Double = setCoordinates().lon   //11.327591
        let title: String? = dataReceived
        var subtitle: String? = dataReceived

        let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        let region = MKCoordinateRegionMakeWithDistance(coordinate, 1000, 1000)
        mapView.setRegion(region, animated: true)

        // title may be to be taken from iconNames[indexpath.row]
        pin = AnnotationPinn(title: "", subtitle: "", coordinate: coordinate)
    }

    func addDoubleTap() { //not finding numberOfTapsRequired
        let doubleTap = UIGestureRecognizer(target: self, action: #selector(MapViewController.dropPin))
        doubleTap.numberOfTapsRequired = 2
        doubleTap.delegate = self
        mapView.addGestureRecognizer(doubleTap)
    }

    func centerMapOnLocation() {
        guard let coordinate2 = locationManager.location?.coordinate else {
            return
        }
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinate2, regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)

    }





    //custom pin image , named: iconsImages[indexPath.row]
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let annotationView = MKAnnotationView(annotation: pin, reuseIdentifier: "strada chiusa")

        //added if statement for displaying user location blue dot
        if annotation is MKUserLocation{
            return nil
        } else {
        annotationView.image = UIImage(named: dataReceived!) // here choose the image to load

        let transform = CGAffineTransform(scaleX: 0.22, y: 0.22)
        annotationView.transform = transform
        return annotationView
        }
    }

    // get coordinates into variables for the dropPin()
    func setCoordinates() -> ( lat: Double, lon:Double) {
    let location = locationManager.location?.coordinate
    let lat:Double = (location?.latitude)!
    let lon:Double = (location?.longitude)!
     print(lat,lon)
     return (lat, lon)
    }

   func dropPin() {




//           print("3\(String(describing: dataReceived))")
        mapView.addAnnotation(pin)


    }

    @IBAction func dropPinButton(_ sender: Any) {
        performSegue(withIdentifier: "chooseIconSegue", sender: self)
    }

    @IBAction func centerMapButton(_ sender: Any) {
        if authorizationStatus == .authorizedAlways || authorizationStatus == .authorizedWhenInUse{
            centerMapOnLocation()
        }
    }

    @IBAction func unwindHere(sender:UIStoryboardSegue) { // datas coming back

        if let sourceViewController = sender.source as? IconsViewController {
//            MyVariables.dataReceived = sourceViewController.dataPassed
            dataReceived = sourceViewController.dataPassed
//            title = sourceViewController.dataPassed

            print(dataReceived!)
            dropPin()
            mapView.addAnnotation(pin)
        }
    }


    func configureLocationServices() {
        if authorizationStatus == .notDetermined{
            locationManager.requestAlwaysAuthorization()
        } else {
            return
        }
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        centerMapOnLocation()
        }
    }

extension MapViewController: CLLocationManagerDelegate{
    }

最佳答案

UIGestureRecognizer 没有名为 numberOfTapsRequired 的属性,这就是您遇到编译错误的原因。

你要找的是UITapGestureRecognizer .

关于ios - 类型 'UIGestureRecognizer' 的值没有成员 'numberOfTapsRequired' 为什么会出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49862234/

相关文章:

swift - 使用左栏按钮在导航栏上滑动后退手势

iphone - 将 UIPanGesture 翻译发送到数组中的所有 View

ios - 将 PHAsset 转换为 UIImage 后设置 UiButton 图像

iphone - iOS,按下 "back"按钮时停止运行功能

ios - 如何在 Core Data 中为 iOS 开发指定基数

html - 如何使用 swift 在 WKWebView 中预填充网站上的文本框

swift - 如何制作可编辑的表格单元格

ios - 手势触发UIScrollView subview 异常

ios - Xcode 6.2 - 'as' 之后的预期类型

swift - 当 Gifu 完成动画时运行代码