从 swift 中的发件人获取 UIImage
get UIImage from sender in swift
我在 table 单元格中有一个 UIImageView
,我向它添加了一个 tapGesture。
我想在 handleTap 方法中访问 UIImageView
。
这是 TableCell 中图像的代码:
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 ?
}
我想我必须从 UIView
投射它?
试试这个代码。
func handleTap(sender : UITapGestureRecognizer) {
let imgView = sender.view as! UIImageView
// Do something.
}
由于您使用了手势识别器,handleTap
的发件人将是 UITapGestureRecognizer
。手势识别器有你想要的
var view: UIView? { get }
// the view the gesture is attached to. set by adding the recognizer to a UIView using the addGestureRecognizer: method
不,sender
是 UITapGestureRecognizer
而不是 UIView
。你应该这样做:
func handleTap(sender : UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
}
我在 table 单元格中有一个 UIImageView
,我向它添加了一个 tapGesture。
我想在 handleTap 方法中访问 UIImageView
。
这是 TableCell 中图像的代码:
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 ?
}
我想我必须从 UIView
投射它?
试试这个代码。
func handleTap(sender : UITapGestureRecognizer) {
let imgView = sender.view as! UIImageView
// Do something.
}
由于您使用了手势识别器,handleTap
的发件人将是 UITapGestureRecognizer
。手势识别器有你想要的
var view: UIView? { get }
// the view the gesture is attached to. set by adding the recognizer to a UIView using the addGestureRecognizer: method
不,sender
是 UITapGestureRecognizer
而不是 UIView
。你应该这样做:
func handleTap(sender : UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
}