2

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.

mr-sk
  • 13,174
  • 11
  • 66
  • 101
  • 1
    I am not sure exactly how you would do it with a scroll view (possibly with a subclass?), but for a `UIImageView`, all you have to do is set the `userInteractionEnabled` property to YES (it defaults to NO for image views) – Simon Goldeen May 18 '11 at 20:39
  • 1
    Yup, do as Simon said. You can use http://stackoverflow.com/questions/2432176/xcode-how-to-change-image-on-touching-at-an-image-same-position/2434440#2434440 but instead changing the image, run your event code. – Jano May 18 '11 at 20:44
  • Ok, thanks Simon. Jano, that link looks great, Im going to go down that path I think. Thanks, I'll report back my results. – mr-sk May 18 '11 at 20:53

1 Answers1

5

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

justin
  • 5,811
  • 3
  • 29
  • 32
  • It does and actually I might go this route in the end. The prior answer, I'm not sure I need all that. Though it is a really cool way to solve it. – mr-sk May 18 '11 at 21:31
  • I ended up going this route and building a small class that i can query that'll provide the x/y/l/w for the button and i just add to the subview. Working well so far. Thanks to everyone's answers. – mr-sk May 19 '11 at 00:55
  • My pleasure. I'm glad it helped and best of luck with the rest of your application – justin May 19 '11 at 01:38