我在我的视图中使用谷歌地图。我想为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")
}}
有人能告诉我为什么触摸事件不起作用吗?
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")
}
}
答案 0 :(得分:0)
可能视图位于另一个未启用用户交互的UIView中。不是吗?