2015-10-22 71 views
0

我重視的事件對我的UIImageViews與UITapGestureRecognizer查找點擊了的UIImageView從UITapGestureRecognizer

override func viewDidLoad() { 
    super.viewDidLoad() 

    let cupPicture1Tap = UITapGestureRecognizer(target:self, action:Selector("imageTapped:")) 
    cupPicture1.userInteractionEnabled = true 
    cupPicture1.addGestureRecognizer(cupPicture1Tap) 
    let cupPicture2Tap = UITapGestureRecognizer(target:self, action:Selector("imageTapped:")) 
    cupPicture2.userInteractionEnabled = true 
    cupPicture2.addGestureRecognizer(cupPicture2Tap) 
    let cupPicture3Tap = UITapGestureRecognizer(target:self, action:Selector("imageTapped:")) 
    cupPicture3.userInteractionEnabled = true 
    cupPicture3.addGestureRecognizer(cupPicture3Tap) 
    } 

    func cupImageTapped(recognizer:UIPanGestureRecognizer) { 
    var clickedImageView: UIImageView = recognizer.view 

    let myPickerController = UIImagePickerController() 
    myPickerController.delegate = self; 
    myPickerController.sourceType = UIImagePickerControllerSourceType.Camera 
    myPickerController.allowsEditing = true 
    self.presentViewController(myPickerController, animated: true, completion: nil) 
    } 

但我不能訪問的UIImageView點擊在cupImageTapped功能。我得到:「不能轉換'UIView'類型的值?'到指定的類型 '的UIImageView'」

+0

你是不是attched點擊手勢的方法,你attatched的pangesture方法, PLZ更新你的問題 –

回答

1
@IBOutlet var imageView: UIImageView! 
@IBOutlet var MyImageView: UIImageView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // create tap gesture recognizer 
    let tapGesture = UITapGestureRecognizer(target: self, action: "tapGesture:") 

    // add it to the image view; 
    imageView.addGestureRecognizer(tapGesture) 
    // make sure imageView can be interacted with by user 
    imageView.userInteractionEnabled = true   
} 

func tapGesture(gesture: UIGestureRecognizer) { 
    // if the tapped view is a UIImageView then set it to imageview 
    if let imageView = gesture.view as? UIImageView { // if you subclass UIImageView, then change "UIImageView" to your subclass 
     // change the image on the MyImageView 
     MyImageView.image = imageView.image 
     // if you subclass UIImageView, then you could get the filename here. 
    } 
} 

這將工作

2

你需要將其轉換爲UIImageView

if var clickedImageView = recognizer.view as? UIImageView{ 
    //Use clickedImageView here 
} 
相關問題