在 ios 中以编程方式单击 UIImageView 上的事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17120476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:57:40  来源:igfitidea点击:

Click Event on UIImageView programmatically in ios

iphoneiosuiimageviewuitouchuitapgesturerecognizer

提问by Sumit Patel

I am displaying a image from code here is the code

我在这里显示代码中的图像是代码

UIImageView *preArrowImage =[[UIImageView alloc]init ];
preArrowImage.image =[UIImage imageNamed:@"arrowprev.png"];
preArrowImage.frame = CGRectMake(20, 60, 10, 30);
[self.view addSubview:preArrowImage];

I want to handle the touch event on the preArrowImage programmatically.

我想以编程方式处理 preArrowImage 上的触摸事件。

回答by Vivek Sehrawat

Objective-c

目标-c

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[preArrowImage setUserInteractionEnabled:YES];
[preArrowImage addGestureRecognizer:singleTap];

-(void)tapDetected{
    NSLog(@"single Tap on imageview");
  }

SWIFT 4.2/5

斯威夫特 4.2/5

let preArrowImage : UIImageView // also give it frame
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
preArrowImage.isUserInteractionEnabled = true
preArrowImage.addGestureRecognizer(singleTap)

//Action
@objc func tapDetected() {
    print("Imageview Clicked")
}

回答by IronManGill

Simply add a UITapGestureon the image but remember to make its UserInteraction Enabled.

只需UITapGesture在图像上添加一个,但请记住使其UserInteraction Enabled

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self 
    action:@selector(singleTapGestureCaptured:)];
    [preArrowImage addGestureRecognizer:singleTap];
    [preArrowImage setMultipleTouchEnabled:YES];
    [preArrowImage setUserInteractionEnabled:YES];

回答by Sakiboy

Now in Swift!

现在在斯威夫特!

let singleTap = UITapGestureRecognizer(target: self, action: Selector("tapDetected"))
singleTap.numberOfTapsRequired = 1

preArrowImage.userInteractionEnabled = true
preArrowImage.addGestureRecognizer(singleTap)


//Action
func tapDetected() {
    println("Single Tap on imageview")
}

回答by Mohamed Jaleel Nazir

Swift 3

斯威夫特 3

On UIImageView enable UserInterAction

UIImageView 上启用 UserInterAction

 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)

        if let touch = touches.first {
            if touch.view == self.imgVwPostPreview { //image View property

                //Do your actions
                //self.delegate?.didClickOnCellImageView(indexPath: self.cellIndexPath!)
            }
        }
    }

回答by Subhash

Using story board with view in view controller.
view may be ImageView, UIView, UILabel, TextField or other controller

在视图控制器中使用带有视图的故事板。
视图可以是 ImageView、UIView、UILabel、TextField 或其他控制器


After selecting one of the gesture recognizer drag it to your controller.
Now it will display in "Document outline" and header of view controller xib.


选择手势识别器之一后,将其拖到您的控制器上。
现在它将显示在“文档大纲”和视图控制器 xib 的标题中。

Gesture icon in document out line and xib
Drag gesture to UILable
Drag gesture to UILable

文档输出行和 xib 中的手势图标
拖动手势到 UILable
拖动手势到 UILable



Manually create (IBAction)method in header file

在头文件中手动创建(IBAction)方法


create IBAction method in header file
Now drag it from handler to gesture icon in xib
Link it with gesture icon


在头文件中创建 IBAction 方法
现在将其从处理程序拖动到 xib 中的手势图标
将其与手势图标链接

回答by xxtesaxx

If your UIImageView is an IBOutlet which you get from a Storyboard or Nib, you could add a UIGestureRecognizer to the image view, enable user interaction and connect a IBAction to the gesture recognizer.

如果您的 UIImageView 是从 Storyboard 或 Nib 获得的 IBOutlet,您可以将 UIGestureRecognizer 添加到图像视图,启用用户交互并将 IBAction 连接到手势识别器。

For more info check my answer on this question: How to you make a UIImageView on the storyboard clickable (swift)

有关更多信息,请查看我对这个问题的回答:How to make a UIImageView on the storyboard clickable (swift)

回答by Raki

Here's another option: Cheat.

这是另一种选择:作弊。

I mean, think laterally.

我的意思是,横向思考。

Set up your UIImage how you want, cropped, aspect fit, whatever.

设置你的 UIImage 你想要的方式,裁剪,方面适合,等等。

Overlay this with a UIView (equal dimensions, position etc.)

用 UIView 覆盖它(相等的尺寸、位置等)

Set the background to clearcolour, and the class to UIControl.

将背景设置为 clearcolour,并将类设置为 UIControl。

Point the touch up inside event to your handler, and voila.

将 touch up inside 事件指向您的处理程序,瞧。

回答by Olivier MATROT

Xamarin.iOS

Xamarin.iOS

UIImageView preArrowImage; // also give it frame
var singleTap = new UITapGestureRecognizer(TapDetected);
singleTap.ShouldReceiveTouch -= TapGesture_ShouldReceiveTouch;
singleTap.ShouldReceiveTouch += TapGesture_ShouldReceiveTouch;
preArrowImage.UserInteractionEnabled = true;
preArrowImage.AddGestureRecognizer(singleTap)

bool TapGesture_ShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch)
{
    return true;
}

void TapDetected(UITapGestureRecognizer tapGestureRecognizer)
{
    print("Imageview Clicked")
}