tags:

views:

70

answers:

1

I have a great problem since last 2 days. I'm working with a multiple Touch enabled view. My UIViewController has 8-10 imageview. I want to detect touch on each view separately, but multiple view at a time. Touch is detected on all image view, but problem is here-

Suppose I have Tap on a image view and hold down this image view and now tap another image view, second image view is detected touch successfully but it also trigger to first image view which is previously touched and hold down. But I don't want it. So please any one help me. Code level help is appreciated.

NB. My UIViewController also implemented TouchesMoved Methods for swiping purpose.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for(UITouch *touch in event.allTouches) {
        if(CGRectContainsPoint([imView1 frame], [touch locationInView:self.view])){ 
            NSLog(@"imView 1 touched");
        }
        if(CGRectContainsPoint([imView2 frame], [touch locationInView:self.view])){ 
            NSLog(@"imView 2 touched");
        }
    }
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for(UITouch *touch in event.allTouches) {
        if(CGRectContainsPoint([imView1 frame], [touch locationInView:self.view])){ 
            NSLog(@"imView 1 touch moved");
        }
        if(CGRectContainsPoint([imView2 frame], [touch locationInView:self.view])){ 
            NSLog(@"imView 2 touch moved");
        }
    }
}
A: 

Try subclassing a UIImageView and handle the touches code in that subclass. If you need to pass the touch to the parent for some reason, you can handle the touch then send it on to the parent which would be your view controller.

EDIT:

have your custom class like this

@interface CustomImageView : UIImageView {}@end
@implementation CustomImageView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touch began %d",self.tag);
    [self.nextResponder touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touch moved %d",self.tag);
    [self.nextResponder touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touch ended %d",self.tag);
    [self.nextResponder touchesEnded:touches withEvent:event];
}
@end

and have your parrent code something like this

@implementation trashmeTouchIpadViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"parent Touch Began");
        //do parent stuff
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"parent Touch ended");
        //do parent stuff
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setMultipleTouchEnabled:YES];
    UIImage *i1 = [UIImage imageNamed:@"img1.jpg"];
    CustomImageView *v1 = [[CustomImageView alloc] initWithImage:i1];
    v1.frame = CGRectMake(100, 100, 200, 200);
    v1.userInteractionEnabled = YES;
    v1.multipleTouchEnabled = YES;
    v1.tag = 111;
    [self.view addSubview:v1];
    [v1 release];
    UIImage *i2 = [UIImage imageNamed:@"img2.jpg"];
    CustomImageView *v2 = [[CustomImageView alloc] initWithImage:i2];
    v2.frame = CGRectMake(500, 100, 200, 200);
    v2.userInteractionEnabled = YES;
    v2.multipleTouchEnabled = YES;
    v2.tag = 999;
    [self.view addSubview:v2];
    [v2 release];
}
AtomRiot
Thanks for your quick response. Can you please explain it. How it will work ?
instead of using a UIImageView, create a new NSObject and subclass UIImageView so it would look like @interface MyImageView : UIImageView{}@end and in the implementation (.m file) you would have your @implementation and then your touches code. inside the subclass, you do whatever it is you need to do, and if you need to call something on the parent, then you could do that. to send the touches to the parent you would do something like [self.nextResponder touchesMoved:touches withEvent:event];
AtomRiot
Thanks for your suggestion. If you give me a source code example then it's saves my 2 days. Please give me a example project which have more than 2 imageview and detect swipe on each image view separately and also fixed this issue. Thanks again for reply.
I have implement it. But problem is still arising. I implement touchesMoved in my parent class and when i hold 1 imageview and touches another imageview then sometimes two images fires at the same time. But I need to fire only which i touch again. Not the previously holded imageview. Please can you solve this problem. Thanks
Any touches code you implement in the parent will cause this behavior. You will need to handle that in the subclassed objects. What are your gestures supposed to do?
AtomRiot
Yes, right my parent class touchesMoved method causes this behavior. But touchesMoved is needed for sweeping purpose. So what can I do?