为什么我的UITapGestureRecognizer
没有触发UIAlertAction
的功能?当我为iOS Simulator
构建并运行我的应用时,我的图片将无法识别触发警报的点按。
任何建议将不胜感激。谢谢,斯威夫特朋友们!
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 :(得分:1)
在代码中取消注释此行
// imageView.isUserInteractionEnabled = true
。
可能会奏效。另外添加
image.multipleTouchEnabled = YES;
tap.numberOfTapsRequired = 1;