Ios 触摸甚至不起作用?

Ios 触摸甚至不起作用?,ios,swift,google-maps,uitouch,Ios,Swift,Google Maps,Uitouch,在我看来,我正在使用谷歌地图。我想为ma视图实现touchBegin和touchEnd事件,但不会触发事件 import UIKit import GoogleMaps import GooglePlaces import GooglePlacePicker class Home_ViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate ,UINavigationControllerDeleg

在我看来,我正在使用谷歌地图。我想为ma视图实现touchBegin和touchEnd事件,但不会触发事件

import UIKit
import GoogleMaps
import GooglePlaces
import GooglePlacePicker

class Home_ViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate ,UINavigationControllerDelegate , UICollectionViewDataSource, UICollectionViewDelegate{

//Locals
private var DefaultLocation = CLLocationCoordinate2D(latitude: 6.914023, longitude: 79.887243)
private var CurrentLocation = CLLocationCoordinate2D(latitude: 0.00000, longitude: 0.00000)
var _DefaultClass = ""
//Locals

//UI OutletControls
@IBOutlet weak var barBtn: UIBarButtonItem!
@IBOutlet weak var navigationView: UIView!
@IBOutlet weak var vehicleClassesCollectionView: UICollectionView!
@IBOutlet weak var mapView: GMSMapView!
@IBOutlet weak var destinationButton: UIButton!
//UI OutletControls


override func viewDidLoad() {
    super.viewDidLoad()
    //Setting up Views
    self.view.addSubview(self.mapView)
    self.mapView.addSubview(self.navigationView)
    self.mapView.addSubview(self.vehicleClassesCollectionView)
    self.mapView.addSubview(self.destinationButton)
    self.mapView.userInteractionEnabled = true

}//viewDidLoad

// touch events//
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    NSLog("touch")
    super.touchesBegan(touches, withEvent: event)
    let touch: UITouch = touches.first! as UITouch
    if(touch.view == self.mapView)
    {
        NSLog("touch view")
    }}
导入UIKit
导入谷歌地图
导入谷歌网站
导入GooglePlacePicker
类主视图控制器:UIViewController、GMSMapViewDelegate、CLLocationManagerDelegate、UINavigationControllerDelegate、UICollectionViewDataSource、UICollectionViewDelegate{
//当地人
私有var DefaultLocation=CLLocationCoordinate2D(纬度:6.914023,经度:79.887243)
专用var CurrentLocation=CLLocationCoordinate2D(纬度:0.00000,经度:0.00000)
var\u DefaultClass=“”
//当地人
//用户界面输出控件
@伊巴布顿主义!
@IBVAR导航视图:UIView!
@IBOUTLE弱var车辆类集合视图:UICollectionView!
@IBV映射视图:GMSMapView!
@IBVAR目标按钮:UIButton!
//用户界面输出控件
重写func viewDidLoad(){
super.viewDidLoad()
//设置视图
self.view.addSubview(self.mapView)
self.mapView.addSubview(self.navigationView)
self.mapView.addSubview(self.VehicleClasseCollectionView)
self.mapView.addSubview(self.destinationButton)
self.mapView.userInteractionEnabled=true
}//viewDidLoad
//触摸事件//
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
NSLog(“触摸”)
super.touchsbegind(touchs,withEvent:event)
让触摸:UITouch=touch.first!作为UITouch
if(touch.view==self.mapView)
{
NSLog(“触摸视图”)
}}
有人能告诉我为什么触摸事件不起作用吗

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    NSLog("touchm")
    super.touchesBegan(touches, withEvent: event)
    let touch: UITouch = touches.first! as UITouch
    if(touch.view == self.mapView)
    {

        NSLog("touch viewm")
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    NSLog("touche")
    super.touchesBegan(touches, withEvent: event)
    let touch: UITouch = touches.first! as UITouch
    if(touch.view == self.mapView)
    {

        NSLog("touch viewe")
    }
}



/////**** Location ManagerDelegates ******/////
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if(status != .AuthorizedWhenInUse)
    {
        //Show my place in map
        let camera = GMSCameraPosition.cameraWithLatitude(( self.CurrentLocation.latitude), longitude:( self.CurrentLocation.longitude), zoom: 15)
        self.mapView.camera = camera
        self.mapView?.settings.myLocationButton = true
        self.mapView?.myLocationEnabled = true
        self.mapView?.delegate = self
        //show my location in map end.

    }
}

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

    if let location = locations.first {

        let location = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        _mapsManager.showOnTheMap(location , mapView: self.mapView, gmsMapViewDelegate: self)
        self.locationManager.stopUpdatingLocation();
        self.CurrentLocation = location
     } else{

        //Alert on network error
        let alertController = UIAlertController(title: "Network Error" , message: "Please check your Internet connection.", preferredStyle: .Alert)
        let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertController.addAction(defaultAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }

}//didUpdateLocations
/////**** CollectionView Delegates ******/////
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.ClassData.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    //let CellModels = vehicleClassesCollectionView.dequeueReusableCell(withReuseIdentifier: "cell_class", for: indexPath as IndexPath) as! VehicleClassesCollectionViewCell
    let CellModels = vehicleClassesCollectionView.dequeueReusableCellWithReuseIdentifier("cell_class", forIndexPath: indexPath) as! VehicleClassesCollectionViewCell
    CellModels.vehicleName.text = self.ClassData[indexPath.row].Name

    let params = ["Latitude": Double((self.CurrentLocation.latitude)),
                  "Longitude": Double((self.CurrentLocation.longitude)),
                  "VehicleType": self.ClassData[indexPath.row].Code,
                  "Model": ""
    ]

    self._vehicleModelService.getNearestVehicleByClass(params) { (_estimatedTime) in
        NSLog("getNearestVehicleByClass\(_estimatedTime)")
        if(_estimatedTime != 0.0){
            CellModels.estimate.text = "\(String(format:"%.1f", _estimatedTime))Mins away"
        }else{
             CellModels.estimate.text = "No Vehicles"
        }
    }
    NSLog("defaultclzzz:\(self._DefaultClass)")
    for i in 0..<CachedVehicleImages.count {
        if(self.ClassData[indexPath.row].Code == CachedVehicleImages[i].Code)
        {
            if(self._DefaultClass.lowercaseString == self.ClassData[indexPath.row].Code.lowercaseString){
                if let url = NSURL(string: CachedVehicleImages[i].ImageUrlSecondary) {
                    if let data = NSData(contentsOfURL: url) {
                        CellModels.vehicleImg.image = UIImage(data: data)
                        self.defaultIndexPath = indexPath
                        self.PreClass = self.ClassData[indexPath.row].Code
                    }
                }
            }else{
                if let url = NSURL(string: CachedVehicleImages[i].ImgUrl) {
                    if let data = NSData(contentsOfURL: url) {
                        CellModels.vehicleImg.image = UIImage(data: data)
                    }
                }
            }
        }
    }


    return CellModels
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    //let cell = collectionView.cellForItemAtIndexPath(indexPath)


    if (IsFirstTime)
    {
        let _cell = collectionView.cellForItemAtIndexPath(defaultIndexPath)
        self.PreClass = self.ClassData[indexPath.row].Code
        if(defaultIndexPath != indexPath)
        {
            if(_cell != nil){
                let x = vehicleClassesCollectionView.cellForItemAtIndexPath(defaultIndexPath) as! VehicleClassesCollectionViewCell
                for i in 0..<CachedVehicleImages.count {
                    if(self._DefaultClass.lowercaseString == CachedVehicleImages[i].Code.lowercaseString)
                    {
                        if let url = NSURL(string: CachedVehicleImages[i].ImgUrl) {
                            if let data = NSData(contentsOfURL: url) {
                                x.vehicleImg.image = UIImage(data: data)
                                NSLog("changed-1")
                            }
                        }
                    }
                }
            }

        }
        IsFirstTime = false
    }

    let x = vehicleClassesCollectionView.cellForItemAtIndexPath(indexPath) as! VehicleClassesCollectionViewCell
    for i in 0..<CachedVehicleImages.count {
        if(self._selectedCode.lowercaseString == CachedVehicleImages[i].Code.lowercaseString)
        {

            if let url = NSURL(string: CachedVehicleImages[i].ImageUrlSecondary) {
                if let data = NSData(contentsOfURL: url) {
                    x.vehicleImg.image = UIImage(data: data)
                      NSLog("changed-2")
                }
            }
        }
    }
    //
    if(PrevIndexPath != nil  )
    {
        let x = vehicleClassesCollectionView.cellForItemAtIndexPath(PrevIndexPath!) as! VehicleClassesCollectionViewCell
        for i in 0..<CachedVehicleImages.count {
            if(self.PreClass.lowercaseString == CachedVehicleImages[i].Code.lowercaseString)
            {
                if let url = NSURL(string: CachedVehicleImages[i].ImgUrl) {
                    if let data = NSData(contentsOfURL: url) {
                        x.vehicleImg.image = UIImage(data: data)
                          NSLog("changed-3")
                    }
                }
            }
        }
        //
    }
    PrevIndexPath = indexPath
    self.classChanged = false
}

var PrevIndexPath:NSIndexPath?
var PreClass: String = ""
var IsFirstTime = true
var defaultIndexPath = NSIndexPath(forRow: 0, inSection: 0)
var classChanged = false
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath){
    collectionView.allowsMultipleSelection = false

    self.PreClass = self._selectedCode
    self._selectedCode = self.ClassData[indexPath.row].Code
    //load vehicles for _slelctedClass
    let params = ["Latitude": Double((self.CurrentLocation.latitude)),
                  "Longitude": Double((self.CurrentLocation.longitude)),
                  "VehicleType": self._selectedCode,
                  "Model": ""
    ]
    self._vehicleModelService.GetAllAvaliableVehiclesModelList(params){ (_AllAvaliableVehiclesModelList) -> () in

        if(_AllAvaliableVehiclesModelList.count != 0)
        {
            let Count = (_AllAvaliableVehiclesModelList.count) as Int
            //Set default vehicle image

            self._mapService.clearMarkersOnTheMap(self.markers)
            for i in 0..<Count  {
                let marker = self._mapService.ShowMultipleMarkers(CLLocationCoordinate2D(latitude: _AllAvaliableVehiclesModelList[i].Latitude,longitude: _AllAvaliableVehiclesModelList[i].Longitude),markerIconUrl: self._mapService.getMarkerIconByClass(self._selectedCode), mapView: self.mapView)
                self.markers.append(marker)
            }
        }else{
            //Show no vehicles
            self._mapService.clearMarkersOnTheMap(self.markers)
        }

    }
    self.classChanged = true
}



@IBAction func DestinationBtnAction(sender: AnyObject) {
    NSLog("selectedCls0:\(_selectedCode)")
    self._plistHelper.addValueToPlistFile(self._selectedCode, Key: "SelectedClassHome")
  }
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
NSLog(“touchm”)
super.touchsbegind(touchs,withEvent:event)
让触摸:UITouch=touch.first!作为UITouch
if(touch.view==self.mapView)
{
NSLog(“触摸视图”)
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
NSLog(“触摸”)
super.touchsbegind(touchs,withEvent:event)
让触摸:UITouch=touch.first!作为UITouch
if(touch.view==self.mapView)
{
NSLog(“触摸视图”)
}
}
/////****位置管理者******/////
func locationManager(经理:CLLocationManager,didChangeAuthorizationStatus:CLAuthorizationStatus){
如果(状态!=.AuthorizedWhenInUse)
{
//在地图上显示我的位置
让camera=GMSCameraPosition.cameraWithLatitude((self.CurrentLocation.latitude),经度:(self.CurrentLocation.latitude),缩放:15)
self.mapView.camera=摄影机
self.mapView?.settings.myLocationButton=true
self.mapView?.myLocationEnabled=true
self.mapView?.delegate=self
//在地图末尾显示我的位置。
}
}
func locationManager(管理器:CLLocationManager,didUpdateLocations位置:[CLLocation]){
如果let location=locations.first{
让location=CLLocationCoordinate2D(纬度:location.coordinate.lation,经度:location.coordinate.longitude)
_mapsManager.showOnTheMap(位置,地图视图:self.mapView,gmsMapViewDelegate:self)
self.locationManager.stopUpdateingLocation();
self.CurrentLocation=位置
}否则{
//网络错误警报
让alertController=UIAlertController(标题:“网络错误”,消息:“请检查您的Internet连接”,首选样式:。警报)
让defaultAction=UIAlertAction(标题:“确定”,样式:。默认,处理程序:nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController,动画:true,完成:nil)
}
}//双更新位置
/////****CollectionView委托******/////
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回self.ClassData.count
}
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{
//让CellModels=VehicleClasseCollectionView.dequeueReusableCell(带有ReuseIdentifier:“cell_class”,for:indexPath作为indexPath)作为!VehicleClasseCollectionViewCell
让CellModels=VehicleClasseCollectionView.dequeueReusableCellWithReuseIdentifier(“cell_类”,forIndexPath:indexPath)作为!VehicleClasseCollectionViewCell
CellModels.vehicleName.text=self.ClassData[indexath.row].Name
设params=[“Latitude”:Double((self.CurrentLocation.Latitude)),
“经度”:双((self.CurrentLocation.Longitude)),
“VehicleType”:self.ClassData[indexath.row]。代码,
“模型”:”
]
self._vehicleModelService.getNearestVehicleByClass(参数){(_estimatedTime)in
NSLog(“getNearestVehicleByClass\(\u estimatedTime)”)
如果(_estimatedTime!=0.0){
CellModels.estimate.text=“\(字符串(格式:%.1f),\u estimatedTime))分钟路程”
}否则{
CellModels.estimate.text=“无车辆”
}
}
NSLog(“defaultclzzz:\(self.\u DefaultClass)”)

对于0中的i..可能视图位于另一个未启用用户交互的UIView中。不是吗?

让location:CGPoint?=touch?.location(在:self.YOURMAPVIEW中)在begin方法中添加这一行。你在使用swift 3.0吗?我在使用swift 2.3什么是“导航视图”,它的框架是什么?不需要这个
self.view.addSubview(self.mapView)
同时设置
self.navigationView.userInteractionEnabled=true,self.VehicleClasseCollectionView.userInteractionEnabled=true,self.navigationView.userInteractionEnabled=true