2016-12-31 117 views
1

爲什麼我的UITapGestureRecognizer不會觸發我的UIAlertAction功能?當我爲iOS Simulator構建並運行我的應用程序時,我的圖像無法識別輕擊以觸發警報。在圖像視圖上點擊時遇到問題

任何建議,將不勝感激。謝謝,Swift的朋友們!


import UIKit 

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    let rect = CGRect(x:20,y:20,width:150,height:100) 
    let imageView = UIImageView(frame:rect) 

    let image = UIImage(named:"image.png") 
    imageView.image = image 

    // imageView.isUserInteractionEnabled = true 
    self.view.addSubview(imageView) 
    let guesture = UITapGestureRecognizer(target: self,action: 
     #selector(ViewController.singleTap)) 
    imageView.addGestureRecognizer(guesture) 
} 


func singleTap() 
{ 
    let alertView = UIAlertController(title: "Heading", message: "Message!", preferredStyle: .Alert) 

    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 
    alertView.addAction(defaultAction) 

    self.presentViewController(alertView, animated: true, completion: nil) 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

} 
+0

在你的圖像視圖中設置'userInteractionEnabled'爲'true' – Paulw11

回答

1

取消註釋這行代碼中的

// imageView.isUserInteractionEnabled = true

可能工作。此外還添加這個

image.multipleTouchEnabled = YES; 
tap.numberOfTapsRequired = 1; 
相關問題