使 UIImage/UIScrollView 的区域“可点击”
所以我有两个问题需要解决:
- 检测 UIImageView 中某个区域的点击。
- 检测 UIScrollView 中某个区域的点击。
我正在考虑两者,因为我有来自客户端的 x/y 坐标,所以我将以编程方式实例化一个 UIButton (自定义/透明)并将其放置在 UIImageView 和 UIImageView 所需的区域上。 UIScrollView。
我还将有一个当用户选择按钮时触发的事件。我正在考虑提供一个标签并使用像这样的签名
- (IBAction) btnPress:(id)sender
,这样我就可以查询发件人
[sender tag]
,然后根据标签做出决定。每个按钮都有一个唯一的 ID。 我的问题是:
- 这是一个好方法吗?什么会更好?
- 我知道如何在 IB 中执行所有这些操作,但不是以编程方式执行。我的印象是连接(对于 IBActions)仅在 IB 中进行,所以我如何将按钮连接到所有代码中的事件(我现在将开始谷歌搜索)。
TIA。
So I have two issues to solve:
- Detect a click on an area in an UIImageView.
- Detect a click on an area in an UIScrollView.
I'm thinking for both, since I have the x/y coordinates from the client, I'm going to programmatically instantiate an UIButton (custom/transparent) and place it over the desired area for both the UIImageView & UIScrollViews.
I will also have an event that fires when the user selects the button. I'm thinking about providing a tag and using a signature like
- (IBAction) btnPress:(id)sender
So I can query the sender
[sender tag]
And then make decisions based on the tag. Each button will have a unique id.
My questions are:
- Is this a good way to do it? What would be better?
- I'm aware of how to do all these things in IB, but not programmatically. I was under the impression connections (for IBActions) are only made in IB, so how can I wire up a button to an event all in code (I'll start googling now).
TIA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想使用 UIButton 来处理触摸,您确实可以创建一个自定义按钮来放置在框架上。如果您采用此路线,要将方法应用于按钮,您必须执行以下操作:
然后,当您想要调用该方法时,它将是您所期望的 IBAction 方法。
其中
[sender tag]
确实会让您指定的按钮按照您的意愿继续操作。话虽这么说,我可能更倾向于在视图上设置 UIGestureRecognizer。基本上,您可以标记 UIImageView 和/或 UIScrollView,然后创建一个手势识别器:
然后处理手势,
当然,如果您有自定义的 scrollView 或 imageView 类,您可以简单地重写
touchesBegan
方法并从那里做你想做的事。我认为这应该很好地涵盖您的选择,所以我希望这会有所帮助If you want to use a UIButton to handle the touches, you can indeed create a custom button to place over the frame. If you take this route, to apply a method to the button, you must do the following:
Then when you want to call the method, it will be an IBAction method as you would expect.
where
[sender tag]
will indeed get your specified button to proceed as you wish.That being said, I might be more inclined to set up a UIGestureRecognizer on the views. Basically, you would tag your UIImageView and/or UIScrollView, then create a gesture recognizer:
Then to handle the gesture,
Of course, if you have custom scrollView or imageView classes, you can simply override the
touchesBegan
method and do what you want from there. I think that should cover your options pretty well, so I hope this helps