开发者

UIImageView Tag and Tap Gesture Issue

开发者 https://www.devze.com 2023-03-30 20:53 出处:网络
I am working on a Kid\'s Book App for iPad. It has a UIView which loads UIImageView to display UIImages (JPEG\'s), user can swipe on images to browse through pages - everything works fine. Now I wante

I am working on a Kid's Book App for iPad. It has a UIView which loads UIImageView to display UIImages (JPEG's), user can swipe on images to browse through pages - everything works fine. Now I wanted to add some interactivity to some of the pages by adding another UIImageView which would load a PNG file and on Tap Gesture I want to animate them... Below is the code snippet...

I added a Tap Gesture to UIView inside viewDidLoad. viewDidLoad calls loadPage and inside loadPage I am programatically adding a UIImageView (imageAnimation) containing a PNG file and also assigning a tag to it so that I can play animations based on tags inside handleTap routine. For some reason, the switch statement in handleTap execute ONLY for case 1, for other cases handleTap routine is NEVER called. What wrong am I doing?

#import "KidsViewController.h"

@implementation KidsViewController
@synthesize imageAnimation;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UISlider class]] || [touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
}

- (void)handleTap:(UITapGestureRecognizer *)recognizer {

    NSLog(@"KidsViewController ==> handleTap.");

    switch (((UIGestureRecognizer *)recognizer).view.tag)      
    {
        case 1:
            //...
            NSLog(@"KidsViewController ==> handleTap. Switch Case: %d", 1);
            break;
        case 2:
            //...
            NSLog(@"KidsViewController ==> handleTap. Switch Case: %d", 2);
            break;
        case 3:
            //...
            NSLog(@"KidsViewController ==> handleTap. Switch Case: %d", 3);
            break;            
        default:
            NSLog(@"KidsViewController ==> handleTap. Switch Case: DEFAULT");
            break;
    }

}

- (void)viewDidLoad {

    pageCount=12;
    pageNum=1;

    //put imageviews in place
    image开发者_如何学GoNext.frame=CGRectMake(0,0-crop,screenwidth,screenheight+(crop*2));
    imageCurrent.frame=CGRectMake(0,0-crop,screenwidth,screenheight+(crop*2));

    [self loadPage];

    imageCurrent.image = [UIImage imageWithContentsOfFile:[self filePathForLanguage:language pageNumber:pageNum fileType:@"jpg"]];

    //TAP GESTURE
    UITapGestureRecognizer *tapRecognizer;
    tapRecognizer=[[UITapGestureRecognizer alloc] 
                   initWithTarget:self
                   action:@selector(handleTap:)];
    tapRecognizer.numberOfTapsRequired=1;
    tapRecognizer.numberOfTouchesRequired=1;
    [self.imageAnimation addGestureRecognizer:tapRecognizer];
    tapRecognizer.delegate = self;
    [tapRecognizer release];
}

-(void)loadPage{

    imageNext.image = [UIImage imageWithContentsOfFile:[self filePathForLanguage:language pageNumber:pageNum fileType:@"jpg"]]; //[UIImage imageWithContentsOfFile:pathFilename];

    switch (pageNum)      
    {
        case 1:
            //...
            NSLog(@"KidsViewController ==> loadPage. Switch Case: %d", pageNum);
            UIImage *image = [UIImage imageNamed:@"P3-stilts_00000.png"];
            CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
            imageAnimation = [[UIImageView alloc] initWithFrame:frame];
            imageAnimation.userInteractionEnabled = YES;
            imageAnimation.image = image;
            imageAnimation.tag = pageNum;
            [self.view addSubview:imageAnimation];
            [image release];
            break;
        case 2:
            //...
            NSLog(@"KidsViewController ==> loadPage. Switch Case: %d", pageNum);
            imageAnimation.image = nil;
            [imageAnimation setCenter:CGPointMake(-100,-100)];
            break;
        case 3:
            //...
            NSLog(@"KidsViewController ==> loadPage. Switch Case: %d", pageNum);
            UIImage *image3 = [UIImage imageNamed:@"bug.png"];
            CGRect bugFrame = CGRectMake(0, 0, image3.size.width, image3.size.height);
            imageAnimation = [[UIImageView alloc] initWithFrame:bugFrame];
            imageAnimation.userInteractionEnabled = YES;
            imageAnimation.image = image3;
            imageAnimation.tag = pageNum;
            [self.view addSubview:imageAnimation];
            [image3 release];
            break;            
        default:
            NSLog(@"KidsViewController ==> loadPage. Switch Case: DEFAULT");
            [imageAnimation setCenter:CGPointMake(-100,-100)];
            break;
    }
}

- (void)dealloc {
    [setupViewController release];
    [imageCurrent release];
    [imageNext release];
    [imageShadow release];
    [imageMenuBar release];
    [imageAnimation release];
    [super dealloc];
}

@end


You always get tag of your self.view. It's tag by default is 0. So switch jumps to default option.

You can add your recognizer to imageAnimation and it will work fine.


Your problem might be here:

switch (((UIGestureRecognizer *)recognizer).view.tag)  

When you register that gesture recognizer you are adding to your view controller's view and not to the imageview you are after:

[self.view addGestureRecognizer:tapRecognizer];

If you are trying to get the view that was tapped, consider using something like

CGPoint point = [tapRecognizer locationInView:tapRecognizer.view];
UIView *viewThatWasTouched = [tapRecognizer.view hitTest:point withEvent:nil];
0

精彩评论

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