使 UIImage/UIScrollView 的区域“可点击”

发布于 2024-11-07 23:22:06 字数 589 浏览 3 评论 0原文

所以我有两个问题需要解决:

  1. 检测 UIImageView 中某个区域的点击。
  2. 检测 UIScrollView 中某个区域的点击。

我正在考虑两者,因为我有来自客户端的 x/y 坐标,所以我将以编程方式实例化一个 UIButton (自定义/透明)并将其放置在 UIImageView 和 UIImageView 所需的区域上。 UIScrollView。

我还将有一个当用户选择按钮时触发的事件。我正在考虑提供一个标签并使用像这样的签名

- (IBAction) btnPress:(id)sender

,这样我就可以查询发件人

 [sender tag]

,然后根据标签做出决定。每个按钮都有一个唯一的 ID。 我的问题是:

  1. 这是一个好方法吗?什么会更好?
  2. 我知道如何在 IB 中执行所有这些操作,但不是以编程方式执行。我的印象是连接(对于 IBActions)仅在 IB 中进行,所以我如何将按钮连接到所有代码中的事件(我现在将开始谷歌搜索)。

TIA。

So I have two issues to solve:

  1. Detect a click on an area in an UIImageView.
  2. 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:

  1. Is this a good way to do it? What would be better?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2024-11-14 23:22:06

如果您想使用 UIButton 来处理触摸,您确实可以创建一个自定义按钮来放置在框架上。如果您采用此路线,要将方法应用于按钮,您必须执行以下操作:

UIButton *myButton = [UIButton buttonWithType:....];
myButton.tag = // your tag;
[myButton addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside];

然后,当您想要调用该方法时,它将是您所期望的 IBAction 方法。

- (IBAction)btnPress:(id)sender {

其中 [sender tag] 确实会让您指定的按钮按照您的意愿继续操作。

话虽这么说,我可能更倾向于在视图上设置 UIGestureRecognizer。基本上,您可以标记 UIImageView 和/或 UIScrollView,然后创建一个手势识别器:

UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc] init];
[myGesture addTarget:self action@selector(frameTouched:)];
// for the case of the imageView;
[myImageView addGestureRecognizer:myGesture];
[myGesture release];

然后处理手势,

- (void)frameTouched:(UIGestureRecognizer *)gesture {
    int myLogicTag = [[gesture view] tag];  // this finds the tag of the view it embodies, in this case your imageView
    // continue with your code;
}

当然,如果您有自定义的 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:

UIButton *myButton = [UIButton buttonWithType:....];
myButton.tag = // your tag;
[myButton addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside];

Then when you want to call the method, it will be an IBAction method as you would expect.

- (IBAction)btnPress:(id)sender {

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:

UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc] init];
[myGesture addTarget:self action@selector(frameTouched:)];
// for the case of the imageView;
[myImageView addGestureRecognizer:myGesture];
[myGesture release];

Then to handle the gesture,

- (void)frameTouched:(UIGestureRecognizer *)gesture {
    int myLogicTag = [[gesture view] tag];  // this finds the tag of the view it embodies, in this case your imageView
    // continue with your code;
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文