UIImageview点击不起作用

时间:2017-07-13 08:15:29

标签: ios swift xcode

我很快乐。我正在为UIIMageview实现click事件的功能。我的代码如下。

override func viewDidLoad() {
    super.viewDidLoad()
    initialize()
    let logout = UIImage(named: "logout.png")
    let location = UIImage(named:"location.png")
    let appIcon = UIImage(named: "logo.png")

    imgLogout.image = logout
    imgLocation.image = location
    appicon.image = appIcon
    // Do any additional setup after loading the view, typically from a nib.

    imgLogout.isUserInteractionEnabled = true
      let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ProductListController.profileImageHasBeenTapped(gesture:)))
    //this is where we add the target, since our method to track the taps is in this class
    //we can just type "self", and then put our method name in quotes for the action parameter


    //finally, this is where we add the gesture recognizer, so it actually functions correctly
    imgLogout.addGestureRecognizer(tapGestureRecognizer)

}


func profileImageHasBeenTapped(gesture: UIGestureRecognizer){
    print("Triggered")

}

我的点击事件未触发。任何身体都可以帮助我。 任何人都将不胜感激。

XCode版本为8.3.3

Swift版本为3.1

4 个答案:

答案 0 :(得分:2)

为什么不使用专为此类用途设计的class AppDelegate: UIResponder, UIApplicationDelegate { var viewControllerStack: [BaseViewController]! } override func viewDidLoad() { super.viewDidLoad() super.appDelegateBase.viewControllerStack.append(self) } func go_To_MainMenu(){ var countOfNumberOfViewCOntrollers = self.appDelegateBase.viewControllerStack.count switch countOfNumberOfViewCOntrollers{ self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil) break; case 2: self.presentingViewController?.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil) break; } }

UIButton

答案 1 :(得分:0)

看起来你的孩子不在前面,所以试试吧:

yourparentview.bringSubview(toFront: imgLogout)

答案 2 :(得分:0)

imageLogOut框架可能超出了超级视图范围,请检查它。使用

self.profileImageHasBeenTapped(gesture:)

而不是

 ProductListController.profileImageHasBeenTapped(gesture:)

答案 3 :(得分:0)

    set user interaction enabled for the image.
    // for creating tap gesture recognizer 
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // create tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")

        // add it to the image view;
        imageView.addGestureRecognizer(tapGesture)
        // make sure imageView can be interacted with by user
        imageView.userInteractionEnabled = true

        //if tap is not working bring image to front view
          self.view.bringSubview(imageView)

    }
    func imageTapped(gesture: UIGestureRecognizer) {
        // if the tapped view is a UIImageView then set it to imageview
        if let imageView = gesture.view as? UIImageView {
            println("Image Tapped")
            //Here you can initiate your new ViewController

            }
        }
    }