ios - 触摸也不起作用?

标签 ios swift google-maps uitouch

我认为我正在使用谷歌地图。我想为 ma View 实现 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")
    }}

有人可以告诉我为什么触摸事件不起作用吗?

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")
  }
}

最佳答案

该 View 可能位于另一个未启用用户交互的 UIView 内。不是吗?

关于ios - 触摸也不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43064645/

相关文章:

java - 识别触发 onTap 调用的 GeoPoint

ios - coreplot 中的 x 轴标签未显示

android - 移动应用程序中的 Dwolla 支付集成

arrays - Swift - 根据值连接两个元组数组

java - Google map API 自行停止工作

android - 在我的 Google map 中心创建十字准线

ios - 使用 UIPopoverPresentationController 时如何为 preferredContentSize 设置动画?

ios - swift 编译时出现 Admob 错误

ios - UITableViewDiffableDataSource - 项目移动到另一个部分时的动画转换

ios - 如何子类化 UICollectionViewFlowLayout 以便可以覆盖 layoutAttributesForElementsInRect