2

I have searched a lot over the net but have not been able to find out anything specific. I guess it is not a very big issue, but I am unable to figure it out.

I am working on a UIImageView "A", and then I add some other UIImageViews as the subview of that "A ImageView". UserInteraction is enabled for all the subViews. Now in TouchBegan method I get the array of subviews of "A ImageView" and compare it with Touch.view. but touch.view is always equal to the parent i.e. "A ImageView" and never returns the touched subView.

So please guide me how to detect which subView is touched, and then further I will be moving that subview to a new location by updating its center.

Thanking you all in advance and looking forward to your response.

Regards,

Junaid Rehmat
  • 305
  • 2
  • 15

2 Answers2

0

Is the interaction of the subviews enabled and if yes, have you tried giving separate tags to each major view and subview that you add dynamically and verify the touch by viewing their tags...

yunas
  • 4,143
  • 1
  • 32
  • 38
  • Thanks yunas for your reply Yes userInteraction is enabled, but I havent tried this tags logic, let me try that and I will update you about the results soon. – Junaid Rehmat May 07 '12 at 13:48
  • I started tagging the subViews from 2 onwards, the touched view has always the tag value 0. That is not even a valid tag for any image view, not even for the parent UIImageView. So it means its always detecting self.view as touched view. – Junaid Rehmat May 07 '12 at 14:25
  • hmmmm i assume you are doing something terribly wrong. I have created a demo project, you may download it from here http://cl.ly/GRnl and on console you will see the tag of self.view as 1000 and imageview as 500 as per settings. – yunas May 08 '12 at 07:14
0

I'm afraid it's wrong to use imageview as buttons, but if you insists to do this, try these:

You can re-write the hitTest method by subclassing an imageview

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hittedView = [super hitTest: point withEvent: event];
    NSLog (@"self=<%p>, hitted=<%p>", self, hittedView);
    return hittedView;
}

add logs to see if the default hitTest returns correct subview.

If it doesn't, I'm afraid you have to write hitTest code by your self:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{
    for (UIView *view in self.subviews) {
        if (CGRectContainsPoint(view.frame, point)) {
           return view;
        }
    }
    return self
}

This document is useful for you: http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html

CardMaster
  • 16
  • 4