UIImageView Tag and Tap Gesture Issue

Discussion in 'iOS Programming' started by rameshmrthy, Aug 29, 2011.

  1. rameshmrthy, Aug 29, 2011
    Last edited: Aug 29, 2011

    macrumors newbie

    Joined:
    Jul 27, 2011
    #1
    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?

    Code:
        #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; // *** pageNum gets incremented/decremented when user swipes left or right on the screen
        	
        	//put imageviews in place
        	imageNext.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
    
    
     
  2. macrumors regular

    Joined:
    Aug 26, 2010
    #2
    So is KidViewController being created for every new page? If I'm reading this correctly you have a possibility of 3 pages?


    Code:
    pageNum=1; // *** pageNum gets incremented/decremented when user swipes left or right on the screen
    So what's the deal is this implemented? If not that's your problem. :)
     
  3. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    1. He isn't creating a new page for every image. He is using it within an NSLog statement, He is just assigning a new image to itself.
    2. Setting it to 1 on the viewDidLoad, just sets his page to start on page 1, I don't see the problem on there :)

    I think the problem here, is this piece of code.
    I never see the tag changing from the recognizer. You're only setting the tag on the imageAnimation.
    Try looking into that.
    Good luck.
     
  4. macrumors regular

    Joined:
    Aug 26, 2010
    #4
    Huh. Sorry I lost you after the first sentence, the rest is too fragmented.

    This is what I'm not following. What are pages? UIView, UIImages, UIImageView.


    You don't? I do not see where he increments or decrements the value. This is crucial in helping as that seems to be the root of the problem.

    Again huh, UIGestureRecognizer holds a read-only reference to the attached view.

    Edit:

    I will be the first to admit I'm not a fan of the way the UIGestureRecognizer is being cast but it's acceptable.
     
  5. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #5
    In his code he says this
    So I guess that's the code we don't see, which obviously makes it harder for us to debug.
    Which also makes his "handletap" and "loadPage" a bit more logic.

    Alltough I am following you on the strange way he is casting this UIGestureRecognizer.
    Shouldn't you wether check through the array of gestureRecognizers on your class. Then check if they conform to the [UIWhateverRecognizer class], and base your actions upon them.
    I have never coded using the "tag" sign. since I think that's just a temp usage for not willing to face Hierarchy in your app.
     
  6. macrumors regular

    Joined:
    Aug 26, 2010
    #6
    His original problem was the fact the tag was not incrementing properly. He posted a comment regarding the code fairly crucial code. Hence the sarcasm\humor in my original post. :) In any event it seems moot as the OP doesn't seem interested in this thread any longer.
     
  7. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #7
    My mistake, I misunderstood his question, and see the problem now about entering case 1 only :)
     

Share This Page