从 UI 中选择图像时执行操作

Performing actions when selecting image from UI

我有一个 UIView:

  1. 图像视图
  2. 文本视图
  3. 滚动视图(它有多个在 运行 时间内动态创建的图像)

我需要做的是:当我 select 图像(第一张图像或滚动视图中的图像)时,弹出窗口 window 将出现,其中包含图像。

我准备了弹出视图,但现在我只需要一种方法来识别用户按下的图像,这样我就可以调用弹出控制器并查看图像。 thx 先进。

简单的解决方案: 将点击手势识别器添加到每个图像视图。 然后在手势识别器选择器中,您可以使用发送者的视图 属性,这是视图手势所附加的。

示例:

    UIImageView* imageView1;

    UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaps:)];

/* The number of fingers that must be on the screen */
 tapGestureRecognizer.numberOfTouchesRequired = 1;

/* The total number of taps to be performed before the gesture is recognized */
tapGestureRecognizer.numberOfTapsRequired = 1;

然后在 handleTaps 中您可以执行以下操作

-(void) handleTaps:(UITapGestureRecognizer*)paramSender
{
   UIImageVIew* seletedImageView = paramSender.view;
 UIImage* image = selectImageView.image; //do whatever you want with image
}

*别忘了设置 imageView.userInteractionEnabled = YES;

swift 测试代码

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var imageView1 : UIImageView
        var image1 : UIImage

        image1 = UIImage(named: "testImage")!
        imageView1 = UIImageView(image: image1)
       imageView1.tag = 1
        imageView1.frame = CGRectMake(10, 10, 100, 100)
        let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
        tapGesture.numberOfTouchesRequired = 1
        tapGesture.numberOfTapsRequired = 1
        imageView1.userInteractionEnabled = true

        imageView1.addGestureRecognizer(tapGesture)

        self.view.addSubview(imageView1)
    }
    func handleTap(sender: UITapGestureRecognizer) {
        var imageView : UIImageView = sender.view as UIImageView
        var image : UIImage = imageView.image!
        println("Taped UIImageVIew"+String( imageView.tag))
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}