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.
}
}
在你的圖像視圖中設置'userInteractionEnabled'爲'true' – Paulw11