0

I have an app in which the user can interact with many objects. These are several UIButtons, a few UILabels and a lot of UIImageViews.

The focus of all interaction centers around touching the UIImageView objects. With a touch I can move the images around, tell them to do this or that. However, my current obstacle is in knowing how to properly have the app distinguish touches that occur when I touch a UIButton.

Why? Well the logic within the Touches Began event is meant to be only for the UIImageViews, however the moment that I touch a button, or any other object, the app is interpreting the touch as if it occurred for a UIImageView object.

So, my approach boils down to: is there a good way of identifying if a touch occurred for a UIButton, UIImageView, UILabel object? That way I can filter out the irrelevant touches in my app from the relevant ones.

EDIT:

The code below outlines how I capture the touch event, however I do not know how to know from the touch event whether it is a button or a view that I touched.

touch = [touches anyObject];
touchLocation = [touch locationInView:[self view]];
AGE
  • 3,752
  • 3
  • 38
  • 60
  • Show your touch handling code. Are you hit testing to find the touched view? – Wain Aug 04 '13 at 22:12
  • The code is quite complex as it handles and captures the touches based on the UIImageViews, which is working properly. The problem is that I have no idea how to distinguish touching a UIImage from a UIButton. Does the touch event contain a way to identify this? – AGE Aug 05 '13 at 02:18
  • 1
    How about gestureRecognizer:shouldReceiveTouch: from the gesture recognizer delegate reference; "This method is called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch." You can use this to filter out any unwanted touches before the recognizer gets them. I have a feeling this is happening too late for you, but maybe it will work. – timquinn Aug 05 '13 at 09:06

3 Answers3

1

Call hitTest:withEvent: on the view with the touch event to get the view that is actually being touched. Then you can use isKindOfClass: to check what type of view it is and respond accordingly.

Wain
  • 118,658
  • 15
  • 128
  • 151
1

you can use this method :-

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointInside:point withEvent:event]) {
      //You clicked inside the object
    }
    return [super hitTest:point withEvent:event]
}

and wain has already given you the explanation for it..

https://stackoverflow.com/a/18051856/1865424

Community
  • 1
  • 1
Kundan
  • 3,084
  • 2
  • 28
  • 65
  • I will look into this solution, it is pretty broad though. Is there any way that one can use the above approach to filter out touches that occur only in UIImageView Objects? I simply have never used this approach before. Also I am on vacation, limited internet access that is why I have been late in replying back. – AGE Aug 06 '13 at 17:26
  • 1
    @AGE sorry there is nothing particular for that..but you can check this answer too..http://stackoverflow.com/a/2793253/1865424 – Kundan Aug 07 '13 at 06:45
1

To know whether UIButton is pressed follow this:

-(void) touchBegan : (NSSet *) touches withEvent : (UIEvent *) even
{

 UITouch *touch = [touched anyObject];
 UIView *touchedView = [touch view];

 if([touchedView isMemberofClass : [UIButton class])
 {
   //do something when button is touched
 }
}
user2677941
  • 38
  • 1
  • 8