我在 scrollVIew 上循环创建图像,我需要在单击每个图像时控制动作。我听说可以在图像上设置点击手势,但我不知道如何将点击手势设置为我之前通过循环创建的图像。是否可以在循环中的每个图像上创建点击手势?如何?
问问题
352 次
2 回答
1
Swift 中的相同功能是,
@IBOutlet weak var img1: UIImageView!
@IBOutlet weak var img2: UIImageView!
@IBOutlet weak var img3: UIImageView!
let imgs = [img1, img2, img3]
for (index, img) in images.enumerated() {
img.tag = index
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
img.isUserInteractionEnabled = true
img.addGestureRecognizer(tapGesture)
}
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer){
let img : UIImageView = tapGestureRecognizer.view as! UIImageView
switch(img.tag) {
{
case 0:
//your code
break;
...
}
}
希望这可以帮助某人。
于 2018-05-22T16:50:16.210 回答
0
尝试这个
for(int i=0;i<20;i++)
{
UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourimage.png"]];
[img setTag:i];
img.frame= //set frame accordingly;
img.userInteractionEnabled = YES;
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[img addGestureRecognizer:tap];
[tap release];
[scrollView addSubView:img];
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
UIImageView *imageView = (UIImageView *)recognizer.view;
switch([imageView tag])
{
case 1:
//do your work
break;
.
.
.
.
case n:
}
}
于 2013-06-13T04:53:23.133 回答