基本上,问题与此相同: How to detect touches on UIImageView of UITableViewCell object in the UITableViewCellStyleSubtitle style
但我必须在Swift中这样做。我无法在Obejctive-C中编程。
cell.imageView.userInteractionEnabled = YES;
cell.imageView.tag = indexPath.row;
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
[tapped release];
-(void)myFunction :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(@"Tag = %d", gesture.view.tag);
}
有人可以帮我移植这段代码吗? :(
答案 0 :(得分:4)
尝试自己写。这就是你学习的方式。以下是一些逐行提示:
1: cell.imageView.userInteractionEnabled = YES;
2: cell.imageView.tag = indexPath.row;
3: UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
4: tapped.numberOfTapsRequired = 1;
5: [cell.imageView addGestureRecognizer:tapped];
6: [tapped release];
7: -(void)myFunction :(id) sender
8: {
9: UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
10: NSLog(@"Tag = %d", gesture.view.tag);
11:}
提示
true
代替YES
获得肯定Boolean
let tapped = UITapGestureRecognizer(
并查看弹出的自动完成选项。选择target
和action
的那个。填写目标self
和操作"myFunction:"
addGestureRecognizer(tapped)
cell.imageView
myFunction
函数,该函数调用UITapGestureRecognizer
gesture
并且不返回任何内容println
代替NSLog
并使用字符串插值打印gesture.view.tag
答案 1 :(得分:2)
Swift 3
cell.imageView.isUserInteractionEnabled = true
cell.imageView.tag = indexPath.row
let tapped = UITapGestureRecognizer(target: self, action: #selector(myFunction))
tapped.numberOfTapsRequired = 1
cell.imageView.addGestureRecognizer(tapped)
func myFunction(gesture: UITapGestureRecognizer) {
print("it worked")
}