简体   繁体   中英

detect a touch down event on UIImageView(s)

I placed 4 UIImageView on UIView and I named them

UIImageView myImg1 = [[UIImageView alloc] init];     
UIImageView myImg2 = [[UIImageView alloc] init];     
UIImageView myImg3 = [[UIImageView alloc] init]; 
UIImageView myImg4 = [[UIImageView alloc] init];

How can I tell the compiler that I have just touched down UIImageView 1 or UIImaveView 2. If I only can use UIImageView to do this task, not buttons at all. Please guide me about this issue. Thanks

A stylish solution is to use the UIGestureRecognizer object:

in your loadView method (or anywhere you code the UIImageView (s) drawing...):

   UITapGestureRecognizer *tapRecognizer;
    UIImageView *anImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    [anImageView setTag:0]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
    [anImageView setBackgroundColor:[UIColor redColor]];
    [anImageView setUserInteractionEnabled:TRUE];
    tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTapped:)] autorelease];
    tapRecognizer.numberOfTapsRequired = 1;
    [anImageView addGestureRecognizer:tapRecognizer];
    [contentView addSubview:anImageView];
    [anImageView release];

    UIImageView *anotherImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 180, 100, 100)];
    [anotherImageView setTag:1]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
    [anotherImageView setBackgroundColor:[UIColor greenColor]];
    [anotherImageView setUserInteractionEnabled:TRUE];
    tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTapped:)] autorelease];
    tapRecognizer.numberOfTapsRequired = 1;
    [anotherImageView addGestureRecognizer:tapRecognizer];
    [contentView addSubview:anotherImageView];
    [anotherImageView release];

This is a simple imageViewDidTapped: sample method that you can use to distinguish between the two sample UIImageView objects above:

- (void)imageViewDidTapped:(UIGestureRecognizer *)aGesture {
    UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)aGesture;

    UIImageView *tappedImageView = (UIImageView *)[tapGesture view];

    switch (tappedImageView.tag) {
        case 0:
            NSLog(@"UIImageView 1 was tapped");
            break;
        case 1:
            NSLog(@"UIImageView 2 was tapped");
            break;
        default:
            break;
    }
}

You should consider using the tag property for this. Give each image a unique tag , then use this information to determine which image was touched. For example, set the tags by

UIImageView myImg1 = [[UIImageView alloc] init];     
myImg1.tag = 1;

and when you receive a touch for img , call

img.tag

to retrieve the tag.

UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        singleFingerTap.numberOfTapsRequired = 1;
        [myImg1 addGestureRecognizer:singleFingerTap];
        [myImg2 addGestureRecognizer:singleFingerTap];
        [singleFingerTap release];

And action method

-(void)handleSingleTap:(UIGestureRecognizer*)sender{
    //your code here
}

You can do like this.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [touches anyObject];

       CGPoint touchLocation = [touch locationInView:self.view];
   if(CGRectContainsPoint(myImg1.view.frame,touchLocation))
 {
NSLog(@" Image 1");
}
else if(CGRectCont..

Note..the NSLog will be outputted if the point lies in more than one image view.. in that case..check which image view is on top..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM