Is there an way to make an invisible UIButton that will still quot;be therequot; and catch touch events for my UIImageView?(有没有办法制作一个仍然“在那里的不可见 UIButton?并为我的 UIImageView 捕捉触摸事件?)
问题描述
我认为我很聪明,只是在具有确切帧大小的 UIImageView 上放置了一个透明的 UIButton,这样我就可以轻松地将它与我喜欢的任何事件连接起来,例如 TouchUpInside,并让它调用一个动作方法当用户触摸它时查看控制器.好吧,它一直有效,直到 alpha 低于 0.1f.如果我做 0.01f,它将不起作用.所以为了让它发挥作用,当你在屏幕上看很长时间时,你会看到 0.1f 的 alpha 透过.这太恶心了;)
I thought to be clever and just put an transparent UIButton over an UIImageView with the exact frame size, so that I can wire it up easily with any event I like, for example TouchUpInside, and make it call a action method of an view controller when the user touches it. Well, it works until alpha is below 0.1f. If I do 0.01f, it will not work. So to get it work, when looking a long time on the screen, you'll see that 0.1f of alpha shining through. And that's totally disgusting ;)
iPhone OS 似乎很聪明,如果按钮在视觉上不存在,它就不会捕捉到按钮上的事件.知道如何解决吗?
It seems like iPhone OS trys to be clever and won't catch events on the button if it's visually not there. Any idea how to solve that?
当然,我可以创建 UIImageView 的子类并实现 touchesBegan:... 等,但感觉不是很优雅.我的意思是......当我想在网络上超链接一张图片时,我永远不想为该图片创建我自己的 HTML 元素,只是在点击它时将它连接到一个 url.这对我来说没有多大意义.
Sure I could make a subclass of UIImageView and implement touchesBegan:... etc., but it doesn't feel really elegant. I mean...when I want to hyperlink an image on the web, I would never want create my own HTML element for that image, just to wire it up to an url when it's clicked. That just doesn't make a lot of sense to me.
推荐答案
您应该可以在 Interface Builder 中将按钮的类型"设置为自定义,并且它不会在 UIImageView 上显示任何文本或图形元素.这样,您无需调整 alpha.如果视图是从代码构建的,请使用:
You should be able to set the button's 'Type' to Custom in Interface Builder, and it will not display any text or graphical elements over the UIImageView. This way, you don't need to adjust the alpha. If the view is built from code, use:
button = [UIButton buttonWithType:UIButtonTypeCustom];
这篇关于有没有办法制作一个仍然“在那里"的不可见 UIButton?并为我的 UIImageView 捕捉触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法制作一个仍然“在那里"的不可见 UIButton?并为我的 UIImageView 捕捉触摸事件?
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 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