在swift中从发件人那里获取UIImage

时间:2015-07-10 01:47:49

标签: swift casting uitapgesturerecognizer

我在表格单元格中有一个 <script src="https://maps.googleapis.com/maps/api/js?v=3"></script> <div id="map"></div>,我添加了一个tapGesture。 我想访问handleTap方法中的UIImageView

这是TableCell中Image的代码:

UIImageView

我想我必须从func setImageForCell(cell:ImageCell, indexPath:NSIndexPath) { var image : UIImage = UIImage(named: "brunnen1")! cell.customImageView.userInteractionEnabled = true cell.imageView!.tag = indexPath.row; var tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("handleTap:")) tapGestureRecognizer.numberOfTapsRequired = 1; cell.customImageView.addGestureRecognizer(tapGestureRecognizer) cell.customImageView.image = image } func handleTap(sender : UIView) { // get the UIImageview from the sender, i guess ? } 投出来吗?

2 个答案:

答案 0 :(得分:8)

试试这段代码。

func handleTap(sender : UITapGestureRecognizer) {
    let imgView = sender.view as! UIImageView
    // Do something.
}

由于您使用的是手势识别器,handleTap的发件人将为UITapGestureRecognizer。手势识别器具有您想要的功能。

  

var view: UIView? { get } //手势附加到的视图。通过使用addGestureRecognizer:方法

将识别器添加到UIView来设置

答案 1 :(得分:0)

不,senderUITapGestureRecognizer而不是UIView。你应该这样做:

func handleTap(sender : UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
}