Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 29, 2011, 10:18 AM   #1
rameshmrthy
macrumors newbie
 
Join Date: Jul 2011
UIImageView Tag and Tap Gesture Issue

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

Last edited by rameshmrthy; Aug 29, 2011 at 10:44 AM.
rameshmrthy is offline   0 Reply With Quote
Old Aug 29, 2011, 09:45 PM   #2
Sykte
macrumors regular
 
Join Date: Aug 2010
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.
__________________
Looking for apple/tech news on your IPhone or IPod touch, Try out PowerFeed. 100% free.
Sykte is offline   0 Reply With Quote
Old Aug 30, 2011, 03:19 AM   #3
jnoxx
macrumors 65816
 
jnoxx's Avatar
 
Join Date: Dec 2010
Location: Aartselaar // Antwerp // Belgium
Quote:
Originally Posted by Sykte View Post
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.
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.
Quote:
switch (((UIGestureRecognizer *)recognizer).view.tag)
{
I never see the tag changing from the recognizer. You're only setting the tag on the imageAnimation.
Try looking into that.
Good luck.
jnoxx is offline   0 Reply With Quote
Old Aug 30, 2011, 07:25 PM   #4
Sykte
macrumors regular
 
Join Date: Aug 2010
Quote:
Originally Posted by jnoxx View Post
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.
Huh. Sorry I lost you after the first sentence, the rest is too fragmented.

Quote:
user can swipe on images to browse through pages
This is what I'm not following. What are pages? UIView, UIImages, UIImageView.


Quote:
Originally Posted by jnoxx View Post
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
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.

Quote:
Originally Posted by jnoxx View Post
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.
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.
__________________
Looking for apple/tech news on your IPhone or IPod touch, Try out PowerFeed. 100% free.
Sykte is offline   0 Reply With Quote
Old Aug 31, 2011, 02:43 AM   #5
jnoxx
macrumors 65816
 
jnoxx's Avatar
 
Join Date: Dec 2010
Location: Aartselaar // Antwerp // Belgium
Quote:
Originally Posted by Sykte View Post
Deleted for space usage.
In his code he says this
Quote:
pageNum=1; // *** pageNum gets incremented/decremented when user swipes left or right on the screen
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.
jnoxx is offline   0 Reply With Quote
Old Aug 31, 2011, 06:54 AM   #6
Sykte
macrumors regular
 
Join Date: Aug 2010
Quote:
Originally Posted by jnoxx View Post
So I guess that's the code we don't see, which obviously makes it harder for us to debug.
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.
__________________
Looking for apple/tech news on your IPhone or IPod touch, Try out PowerFeed. 100% free.
Sykte is offline   0 Reply With Quote
Old Aug 31, 2011, 06:56 AM   #7
jnoxx
macrumors 65816
 
jnoxx's Avatar
 
Join Date: Dec 2010
Location: Aartselaar // Antwerp // Belgium
Quote:
Originally Posted by Sykte View Post
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.
My mistake, I misunderstood his question, and see the problem now about entering case 1 only
jnoxx is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPad Mini: App tag in safari issue Thetonyk123 iPad 2 Jan 9, 2013 02:58 PM
Weird UIImageView ContentMode Issue isthisonetaken iPhone/iPad Programming 0 Dec 20, 2012 04:46 PM
Safari back gesture repaint issue sgjohnston OS X 10.8 Mountain Lion 1 Dec 13, 2012 05:09 PM
Interesting TV Show Tag Issue, Please help ngenerator Mac Applications and Mac App Store 0 Oct 14, 2012 08:08 PM
iTunes issue (possible ID3 tag) anton1s Mac Applications and Mac App Store 7 Jul 12, 2012 05:31 PM

Forum Jump

All times are GMT -5. The time now is 10:25 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC