有没有办法制作一个仍然“在那里"的不可见 UIButton?并为我的 UIImageView 捕捉触摸事件?

Is there an way to make an invisible UIButton that will still quot;be therequot; and catch touch events for my UIImageView?(有没有办法制作一个仍然“在那里的不可见 UIButton?并为我的 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 捕捉触摸事件?

基础教程推荐