UITapGestureRecognizer不起作用

时间:2015-08-19 17:08:07

标签: swift uitapgesturerecognizer

我想设置UITapGestureRecognizer以允许用户将图像添加为配置文件。我设置了代码但是,当我触摸图像时,没有任何反应。代码如下:

@IBAction func selezionaFoto(sender: UITapGestureRecognizer) {

    fieldNome.resignFirstResponder()

    func selezionaLibreria(action : UIAlertAction!) {
        UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: true)
        CameraManager.sharedInstance.newImageFromLibraryForController(self, editing: false)
    }

    func scattaFoto(action : UIAlertAction!) {
        UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: true)
        var circle = UIImageView(frame: UIScreen.mainScreen().bounds)
        circle.image = UIImage(named: "overlay")
        CameraManager.sharedInstance.newImageShootForController(self, editing: false, overlay:circle)
    }

    var myActionSheet = UIAlertController(title: NSLocalizedString("ACTION_IMAGE_TITLE", comment: ""),
        message: NSLocalizedString("ACTION_IMAGE_TEXT", comment: ""),
        preferredStyle: UIAlertControllerStyle.ActionSheet)

    myActionSheet.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_LIBRARY", comment: ""),
        style: UIAlertActionStyle.Default,
        handler: selezionaLibreria))

    myActionSheet.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_SHOOT", comment: ""),
        style: UIAlertActionStyle.Default,
        handler: scattaFoto))

    myActionSheet.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment: ""),
        style: UIAlertActionStyle.Cancel,
        handler: nil))

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

}


func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder() 
    return true
}

func incomingImage(image: UIImage) {
    UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
    immagine.image = image
    immagineSelezionata = image
}

func cancelImageSelection() {
    UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
}

故事板中的第一个元素是图像,然后是文本字段。

2 个答案:

答案 0 :(得分:0)

我并非100%确定我理解您的问题,但看起来您只是在故事板上创建UITapGestureRecognizer并将操作连接到它。

我认为您的意思是将UITapGestureRecognizer添加到UIImageView,您可以这样做:

var myImageview = UIImageView()

var myTapGestureRecognizer = UITapGestureRecognizer()

myImageview.addGestureRecognizer(myTapGestureRecognizer)

myTapGestureRecognizer.addTarget(self, action: Selector("methodName:"))

myImageview.userInteractionEnabled = true

func methodName(sender: UITapGestureRecognizer) {

// do stuff
}

答案 1 :(得分:0)

我最近遇到了同样的问题,我的视图层次结构很复杂,并且手势无法响应该操作,原因是将一个superView设置为isUserInteractionEnabled设置为false,并且您可能知道该应用程序如何接收手势以及转到链中的响应者,是该链中的所有UIResponders都应为isUserInteractionEnabled = true,以便传递到最顶视图。我假设您面临相同的情况,因此只需检查层次结构中的所有视图即可。