0

There is an object called "imageView", I need to call the function "clicouCrosshair" always when the "imageView" is clicked, but it's not working. I've tryed some different tutorials that I found on internet but no success. Anyone know why and how can I solve this?

I think the problem is because i'm using my view as a GMSMapView.. Inside the "prepareMap" function the "UIImageView" is configured.

import UIKit
import GoogleMaps

class Maps_criarArea: UIViewController {

    var projNome:String!;
    var mapView:GMSMapView!;

    var imageView:UIImageView!;

    func clicouCrosshair(sender: AnyObject){
        print("CROSSHAIR");
    }

    override func viewDidLoad() {
        super.viewDidLoad();


        GMSServices.provideAPIKey("AIzaSyB62KDZSGfbbN1IIVnlhewi4PpEZmxPJYM");


        let centerBR_lat = -15.30;
        let centerBR_lng = -49.57;

        let cameraP = GMSCameraPosition.cameraWithLatitude(centerBR_lat, longitude: centerBR_lng, zoom: 4);
        mapView = GMSMapView.mapWithFrame( CGRectZero, camera: cameraP)
        mapView.myLocationEnabled = true
        view = mapView;

        self.prepareMap();
    }

    func prepareMap(){

        let imageName = "crosshair";
        let image = UIImage(named: imageName);
        imageView = UIImageView(image: image!);

        let screenSize: CGRect = UIScreen.mainScreen().bounds;
        let WidthPosition = (screenSize.width*0.5)-32;
        let heightPosition = (screenSize.height*0.5)-32;

        imageView.frame = CGRect(x: WidthPosition, y: heightPosition, width: 64, height: 64);
        imageView.userInteractionEnabled = true;


        let detectTap = UITapGestureRecognizer(target: self, action: #selector(self.clicouCrosshair(_:))  )
        //detectTap.numberOfTapsRequired = 1


        imageView.addGestureRecognizer(detectTap)

        mapView.addSubview(imageView);







    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }



    //returning to view
    override func viewWillAppear(animated: Bool) {
        let saveBtn : UIBarButtonItem = UIBarButtonItem(title: "Salvar", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(Maps_criarArea.salvar(_:)) )
        self.navigationItem.rightBarButtonItem = saveBtn
    }

    func salvar(sender:UIBarButtonItem){

    }

}
Xidh
  • 582
  • 1
  • 5
  • 19
  • First of all, if you override viewWillAppear then call it super.viewWillAppear(animated) – Yannick May 05 '16 at 19:54
  • All right, did it. But the problem still there :( – Xidh May 05 '16 at 19:57
  • Your selector does not look right. Try #selector(Maps_criarArea.clicouCrosshair(_:)) instead – Yannick May 05 '16 at 20:02
  • Still no success :/ is that possible that the click of the mouse on the simulator is not equals a touch? – Xidh May 05 '16 at 20:07
  • Lot's of people get burned by this. UIImageView interaction is not enabled by default. http://stackoverflow.com/questions/7735247/uitapgesturerecognizer-on-uiimageview-within-uitablevlewcell-not-getting-called/7735403#7735403 – Curmudgeonlybumbly May 05 '16 at 20:10
  • `imageView.userInteractionEnabled = true;` I use this in my code.. I think this is not the problem :/ or the ordem that I used this may be wrong? – Xidh May 05 '16 at 21:37

2 Answers2

1

Your UIImageView is inside your GMSMapView and you tap is always being handled by GMSMapView. Try to ignore the tap in GMSMapView and pass it to the UIImageView. Add UIGestureRecognizerDelegate in you view and use the delegate method gestureRecognizerShouldBegin:. Hope this helps.

mshrestha
  • 788
  • 5
  • 14
0

As mentioned by mshresth the problem was with the recognization of the gestures like touch.

In order to solve this I change my approach to the problem, instead of trying to add a image with a GestureRecognizer inside a UIView that holds the map, i added the UIView inside the main UIView (view).

Add the map as sub view to view:

    // PREPARA O MAPA
    let centerBR_lat = -15.30;
    let centerBR_lng = -49.57;
    let cameraP = GMSCameraPosition.cameraWithLatitude(centerBR_lat, longitude: centerBR_lng, zoom: 4);
    mapView = GMSMapView.mapWithFrame( CGRectZero, camera: cameraP)
    mapView.myLocationEnabled = true
    let screenSize: CGRect = UIScreen.mainScreen().bounds;
    mapView.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height);
    view.addSubview(mapView);

and to add the image with the gestureRecognizer:

    let imageName = "crosshair";
    let image = UIImage(named: imageName);
    let imageView:UIImageView = UIImageView(image: image!);
    imageView.userInteractionEnabled = true;
    imageView.frame = CGRect(x: (screenSize.width*0.5)-32, y: (screenSize.height*0.5)-32, width: 64, height: 64);
    let detectTap = UITapGestureRecognizer(target: self, action: #selector(Maps_criarArea.clicouCrosshair(_:))  )
    //detectTap.numberOfTapsRequired = 1
    imageView.addGestureRecognizer(detectTap)
    view.addSubview(imageView);

the fuction that contains both of the code above just have them inside it.

This way I was able to add the map, add the image and call a function when the image was pressed

Thanks to everybody who helped

Complete code:

import UIKit
import GoogleMaps

class Maps_criarArea: UIViewController {

    var projNome:String!;
    var mapView:GMSMapView!;

    override func viewDidLoad() {

        super.viewDidLoad();


        GMSServices.provideAPIKey("AIzaSyB62KDZSGfbbN1IIVnlhewi4PpEZmxPJYM");

        self.prepareMap();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func prepareMap(){



        // PREPARA O MAPA
        let centerBR_lat = -15.30;
        let centerBR_lng = -49.57;
        let cameraP = GMSCameraPosition.cameraWithLatitude(centerBR_lat, longitude: centerBR_lng, zoom: 4);
        mapView = GMSMapView.mapWithFrame( CGRectZero, camera: cameraP)
        mapView.myLocationEnabled = true
        let screenSize: CGRect = UIScreen.mainScreen().bounds;
        mapView.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height);
        view.addSubview(mapView);



        let imageName = "crosshair";
        let image = UIImage(named: imageName);
        let imageView:UIImageView = UIImageView(image: image!);
        imageView.userInteractionEnabled = true;
        imageView.frame = CGRect(x: (screenSize.width*0.5)-32, y: (screenSize.height*0.5)-32, width: 64, height: 64);
        let detectTap = UITapGestureRecognizer(target: self, action: #selector(Maps_criarArea.clicouCrosshair(_:))  )
        //detectTap.numberOfTapsRequired = 1
        imageView.addGestureRecognizer(detectTap)
        view.addSubview(imageView);

    }

    func clicouCrosshair(sender: AnyObject){
        print("CROSSHAIR");
    }





    //returning to view
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        let saveBtn : UIBarButtonItem = UIBarButtonItem(title: "Salvar", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(Maps_criarArea.salvar(_:)) )
        self.navigationItem.rightBarButtonItem = saveBtn
    }

    func salvar(sender:UIBarButtonItem){
        print("salvar");
    }


}
Xidh
  • 582
  • 1
  • 5
  • 19