I have a Viewcontroller
with two UIViews
, one named textureView
and one objectView
.
When I only have one view I detect touch using this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *allTouches = [touches allObjects];
for (UITouch *touch in allTouches)
{
gestureStartPoint = [touch locationInView:self.view];
if([touch view] == controlUIImageview){
NSLog(@"TOUCH DETECT");
}
}
}
How do I write this part if I have some things I want to interact with in the textureView
and some in the objectView
?
Do I need to set the textureView
andcontrolView
to setUserInteractionEnabled:TRUE
?
Thanks!
Other way to do this:
gestureStartPoint = [touch locationInView:self.view];
if(CGRectContainsPoint(textureView.frame, gestureStartPoint)){
...
}
if(CGRectContainsPoint(objectView.frame, gestureStartPoint)){
...
}
Try to add a tag
for each subviews you want to add....like:
textureView.tag=0;
objectView.tag=1;
then:
for (UITouch *touch in allTouches)
{
if(touch.view.tag==0){
//...do your stuff with textureView
}else{
//...do your stuff with objectView
}
}
Yes you need to do that, if you want to detect touch on any view you need to setUserInteractionEnabled:TRUE, or make enable in IB(if you are adding view's from IB).
You can try the Singleton Approach
For example you have UIImageView as subview in TextureView
Make a class e.g object.h and Declare the subview you want to interact with E.g
UIImageView *DisplayPicture
UIView *View;
Once thats done. You can set Tags for each of the Two Views
TextureView.tag=0;
Objectview.tag=1;
Than use this method to get the Touch on the View and than Subviews
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (touch.view.tag > 0) {
NSLog(@"%d",[touch.view.subviews count]);
//Get Reference for Texture view
object.View=Touch.view;
//the index will the position at which u added the Subview(UIImageView) e.g If its the first time you addsubview and index of the subview will be O.
object.DipslayPicture=[touch.view.subviews objectAtIndex:0];
}else{
//Similar for ObjectView
}
NSLog(@"tag=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);
i=touch.view.tag;
}
Than you can use the object to do as you like e.g SetImage
[object.DisplayPicture setImage:@"happiness.png"];
It worked for me :) Hope it helps someone :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With