开发者

Second UITouch Event trigger First UITouch Event why?

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])){ 
            N开发者_C百科SLog(@"imView 2 touch moved");
        }
    }
}


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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜