2017-07-13 29 views
0

我對swift非常陌生。我正在爲UIIMageview實現點擊事件的功能。我的代碼如下。UIImageview點擊不起作用

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

雨燕的版本是3.1

+1

是否有一個特殊的原因,你沒有使用這個按鈕? – Alladinian

+0

我需要那裏的形象。所以我用imageview。什麼是使用imageview的障礙? – Noorul

+1

那麼,不,沒有costraint ...但這就是爲什麼'UIButton'存在 – Alladinian

回答

2

爲什麼不使用UIButton,這是專爲那種用法?

let logout = UIButton(type: .custom) 
logout.setImage(UIImage(named: "logout.png"), forState: .normal) 
logout.addTarget(self, action: #selector(self.performLogout), for: .touchUpInside) 

@objc 
func performLogout() { 
    // Do something 
} 
+0

我需要那裏的形象。所以我用imageview。什麼是使用imageview的障礙? – Noorul

+0

您可以將圖像放入按鈕中。 ImageView設計用於以非常高的性能顯示圖像。它用於製作大jpeg或png。 UIButton在內部使用UIImageView來顯示圖像! – Dean

+0

另外,UIButton從UIControl繼承,設計爲由用戶觸發和控制器。 UIImageView不是。有一個原因,UIImageView有「UserInteractionEnabled」設置爲false,它不是爲此。例如,當您製作繪圖畫布時,有時可以將其設置爲true。 – Dean

0

好像yourchildview不是在前面,所以嘗試:

yourparentview.bringSubview(toFront: imgLogout) 
0

imageLogOut框架可能已經超視圖的邊界時,請確認it.use

self.profileImageHasBeenTapped(gesture:) 

代替

ProductListController.profileImageHasBeenTapped(gesture:) 
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 

      } 
     } 
    }