2016-01-25 46 views
2

我正在慢慢地通過蘋果的學習開發Swift,並且我一直用我的水龍頭手勢碰到問題。我已經重新創建了所有項目編號,並取得了相同的結果。iOS - 在圖像視圖上點擊手勢問題

我給圖片視圖添加了一個輕拍手勢,這應該會從我的電腦打開照片庫。什麼都沒發生。

當我從Apple下載並運行示例文件時,一切正常。當我將Apple的代碼複製並粘貼到我的代碼中時,沒有任何反應。我已經經歷了一切,但我覺得我失去了一些東西。

這是我的代碼。蘋果公司的代碼如下:

import UIKit 

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    // MARK: Properties 
    @IBOutlet weak var nameTextField: UITextField! 
    @IBOutlet weak var mealNameLabel: UILabel! 
    @IBOutlet weak var photoImageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // handle the text fields user input through delegate callbacks 
     nameTextField.delegate = self 

    } 

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

    // MARK: UITextFieldDelegate 
    func textFieldShouldReturn(textField: UITextField) -> Bool { 
     // Hide the Keyboard 
     textField.resignFirstResponder() 
     return true 

    } 
    func textFieldDidEndEditing(textField: UITextField) { 
     mealNameLabel.text = textField.text 
    } 

    // MARK: UIImagePickerControllerDelegate 

    func imagePickerControllerDidCancel(picker: UIImagePickerController) { 
     // dismiss the picker if user cancels 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
     // The info dictionary contains multiple representations of the image, and this uses the original. 
     let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage 

     // Set the photoviewimage to be the selected image 
     photoImageView.image = selectedImage 

     // Dismiss the picker 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

    // MARK: Actions 

    @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) { 
     // Hide the Keyboard 
     nameTextField.resignFirstResponder() 

     // UIImagePickerController is a view controller that lets a user pick media from their photo library. 
     let imagePickerController = UIImagePickerController() 

     // Only Allow pictures to be selected and not taken 
     imagePickerController.sourceType = .PhotoLibrary 

     // make sure the viewcontroller is notified when the user selects an image 
     imagePickerController.delegate = self 

     presentViewController(imagePickerController, animated: true, completion: nil) 

    } 

    @IBAction func setDefaultLabelText(sender: UIButton) { 
     mealNameLabel.text = "Default Text" 
    } 

} 

這裏是蘋果公司代碼:

import UIKit 

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
    // MARK: Properties 

    @IBOutlet weak var nameTextField: UITextField! 
    @IBOutlet weak var mealNameLabel: UILabel! 
    @IBOutlet weak var photoImageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Handle the text field’s user input through delegate callbacks. 
     nameTextField.delegate = self 
    } 

    // MARK: UITextFieldDelegate 

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

    func textFieldDidEndEditing(textField: UITextField) { 
     mealNameLabel.text = textField.text 
    } 

    // MARK: UIImagePickerControllerDelegate 
    func imagePickerControllerDidCancel(picker: UIImagePickerController) { 
     // Dismiss the picker if the user canceled. 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
     // The info dictionary contains multiple representations of the image, and this uses the original. 
     let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage 

     // Set photoImageView to display the selected image. 
     photoImageView.image = selectedImage 

     // Dismiss the picker. 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

    // MARK: Actions 
    @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) { 
     // Hide the keyboard. 
     nameTextField.resignFirstResponder() 

     // UIImagePickerController is a view controller that lets a user pick media from their photo library. 
     let imagePickerController = UIImagePickerController() 

     // Only allow photos to be picked, not taken. 
     imagePickerController.sourceType = .PhotoLibrary 

     // Make sure ViewController is notified when the user picks an image. 
     imagePickerController.delegate = self 

     presentViewController(imagePickerController, animated: true, completion: nil) 
    } 

    @IBAction func setDefaultLabelText(sender: UIButton) { 
     mealNameLabel.text = "Default Text" 
    } 

} 

工作代碼從庵埠

import UIKit 

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 


    // MARK: Properties 
    @IBOutlet weak var nameTextField: UITextField! 
    @IBOutlet weak var mealNameLabel: UILabel! 
    @IBOutlet weak var photoImageView: UIImageView! 



    override func viewDidLoad() { 
     let tapgesture = UITapGestureRecognizer(target: self, action: Selector("imagepressed")) 
     photoImageView.userInteractionEnabled = true 
     photoImageView.addGestureRecognizer(tapgesture) 

     super.viewDidLoad() 

     // handle the text fields user input through delegate callbacks 
     nameTextField.delegate = self 

    } 

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

    // MARK: UITextFieldDelegate 
    func textFieldShouldReturn(textField: UITextField) -> Bool { 
     // Hide the Keyboard 
     textField.resignFirstResponder() 
     return true 

    } 
    func textFieldDidEndEditing(textField: UITextField) { 
     mealNameLabel.text = textField.text 
    } 

    // MARK: UIImagePickerControllerDelegate 

    func imagePickerControllerDidCancel(picker: UIImagePickerController) { 
     // dismiss the picker if user cancels 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
     // The info dictionary contains multiple representations of the image, and this uses the original. 
     let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage 

     // Set the photoviewimage to be the selected image 
     photoImageView.image = selectedImage 

     // Dismiss the picker 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

    // MARK: Actions 


    func imagepressed() { 
     nameTextField.resignFirstResponder() 

     // UIImagePickerController is a view controller that lets a user pick media from their photo library. 
     let imagePickerController = UIImagePickerController() 

     // Only Allow pictures to be selected and not taken 
     imagePickerController.sourceType = .PhotoLibrary 

     // make sure the viewcontroller is notified when the user selects an image 
     imagePickerController.delegate = self 

     presentViewController(imagePickerController, animated: true, completion: nil) 
    } 



/* @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) { 
     let tapgesture = UITapGestureRecognizer(target: self, action: Selector("imagepressed")) 
     photoImageView.userInteractionEnabled = true 
     photoImageView.addGestureRecognizer(tapgesture) 

     // Hide the Keyboard 
     nameTextField.resignFirstResponder() 

     // UIImagePickerController is a view controller that lets a user pick media from their photo library. 
     let imagePickerController = UIImagePickerController() 

     // Only Allow pictures to be selected and not taken 
     imagePickerController.sourceType = .PhotoLibrary 

     // make sure the viewcontroller is notified when the user selects an image 
     imagePickerController.delegate = self 

     presentViewController(imagePickerController, animated: true, completion: nil) 

    } 
    */ 

    @IBAction func setDefaultLabelText(sender: UIButton) { 
     mealNameLabel.text = "Default Text" 
    } 

} 
+1

敲擊手勢識別器在哪裏? – matt

+0

您是否在視圖控制器的storyboard/XIB中添加了tapgesture識別器?如果您尚未將輕拍手勢識別器添加到storyboard/xib,然後將操作方法​​連接到selectImageFromPhotoLibrary。 – iamyogish

+0

輕敲手勢已添加到故事板到ImageView上,然後通過拖放方式連接到// Mark:使用UITapGesture的動作 –

回答

3

不喜歡

的原因,默認情況下的UIImageView userInteraction是false,所以你需要手動啓用

第1步

let tapGesture = UITapGestureRecognizer(target:self, action:Selector("imagePressed")) 
photoImageView.userInteractionEnabled = true // this line is important 
photoImageView.addGestureRecognizer(tapGesture) 

步驟2

func imagePressed() 
{ 
//do Your action here whatever you want 
} 
+0

我在哪裏添加此項?這就是我一直在想的(至少在這些方面,因爲我不想給自己多少功勞),但蘋果開發人員並沒有在任何地方展示這一點。 –

+0

on viewdidload call this –

+0

Andu,我已經嘗試了幾個不同的位置來使代碼沒有成功。隨意像我兩歲的兒子一樣對待我,讓我知道在哪裏放置代碼。另外,我只是複製並粘貼在基金selectuserimagefromlibrary下的代碼粘貼到func imagepressed()? –

0

你在哪裏添加UITapGestureRecognizer的圖像視圖?如果您將其添加到Storyboard中,是否將它連接到選擇器selectImageFromPhotoLibrary:

+0

輕拍手勢已添加到故事板到ImageView上,然後通過拖放方式連接到// Mark:使用UITapGesture的動作 –

5

我發現解決了這個問題的最簡單方法是單擊圖像視圖屬性編輯器中的「用戶交互已啓用」複選框。 Screenshot

+0

本質上,此錯誤來自「添加照片照片」部分中缺少指令#12: 「在」屬性「檢查器中,找到」交互「字段並選中」啓用用戶交互「複選框。 稍後您需要使用此功能才能讓用戶與圖像視圖交互。 – ToneDaBass

+0

在將「手勢識別器」控制器連接到「圖像視圖」控制器之前,您必須檢查真實的「用戶交互已啓用」。 –

0

您的代碼非常好。問題出在UIImageView上。

默認情況下,當您將UIImageView添加到故事板時,「用戶交互」選項未選中。

所以你只需要在故事板中啓用這個選項。

  1. 轉到您的故事板。

  2. 選擇您的UIImageView(你把它命名爲photoimageview)

  3. 現在,在「屬性檢查器」查找下的「互動」,「啓用用戶交互」,它應該是「檢查」

  4. 現在運行你的項目它會工作。

相關問題