如何以编程方式将 UIView 或 UIImageView 与“touch up inside"之类的事件联系起来? [英] How can I programmatically link an UIView or UIImageView with an event like "touch up inside"?

查看:16
本文介绍了如何以编程方式将 UIView 或 UIImageView 与“touch up inside"之类的事件联系起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Interface Builder 只允许我为按钮连接此类事件.但就像在 HTML 中一样,我只想有一个空白的 UIImageView - 只要用户点击它 - 就会调用一个方法.我希望有一些很酷的编程方式,我不知道.

Interface Builder will only allow me to hook up such events for an button. But like in HTML, I just want to haven an blank UIImageView where - as soon as the user taps it - a method is invoked. I hope there is some cool programmatically way of doing that, which I don't know about.

更新:

在创建 UIImageView 的视图控制器中,我尝试这样做:

In my View Controller that creates the UIImageView I tried to do this:

SEL actionSelector = @selector(doSomethingWhenImageIsTouched:);
[self.myUIImageView addTarget:nil action:actionSelector forControlEvents:UIControlEventTouchUpInside];

编译器给了我一个警告,UIImageView 可能无法响应 addTarget:... 我必须做什么才能使其与 UIImageView 一起使用.我在文档中看到 UIImageView 不是从 UIControl 继承的,但是 addTarget: 是 UIControl 的一部分.

The compiler gives me a warning, that UIImageView may not respond to addTarget:... what do I have to do so that it works with an UIImageView. I see in the docs that UIImageView does not inherit from UIControl, but addTarget: is part of UIControl.

更新:

我在创建 UIImageView 后最终创建了一个 UIButton.然后我将该按钮的框架设置为 UIImageView 的框架,并将 alpha 设置为 0.1f.由于某种原因,如果 alpha 为 0.0f,它将不起作用.然后,我做了那个很酷的 addTarget: 事情...

I ended up creating an UIButton after creating the UIImageView. Then I set the frame of that button to the frame of the UIImageView, and alpha to 0.1f. For some reason, it will not work if alpha is 0.0f. And then, I did that cool addTarget: thing...

推荐答案

在 SDK 版本 3.2(我不知道可能更早)中,您可以将手势识别器添加到 UIView.UIGestureRecogniser 是一个抽象类,它允许您定义手势(点击、捏合、滑动等)并将其添加到 UIView 中,当手势识别器识别到手势时,它会在您想要的目标上触发一个动作.

In SDK version 3.2 (could be earlier i donno), you can add gesture recognizers to a UIView. UIGestureRecogniser is an abstract class that allows you to define a gesture (tap, pinch, swipe, etc) and add it to a UIView and when the gesture recognizer, recognizes the gesture, it fires up an action on a target of your desire.

在文档中阅读所有相关信息...:D

read all about it in the docs... :D

这篇关于如何以编程方式将 UIView 或 UIImageView 与“touch up inside"之类的事件联系起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆