How can I programmatically link an UIView or UIImageView with an event like quot;touch up insidequot;?(如何以编程方式将 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"之类的事件联系起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以编程方式将 UIView 或 UIImageView 与“touch up inside"之类的事件联系起来?


基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01