Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Thomas Nilsen
Thomas Nilsen
14,957 Points

Build a Photo Browser iPhone App - Double taps

When we tap on an image, the collectionView delegate function handles that:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

This means the "like" function in the photoCell.m which gets called upon a double-tap is never fired. I'm confused as to how one is supposed to handle something like this. Avoid the delegate and use UIGestures only or is there another way?

I downloaded the final project and the double tap didn't work there either. So is this a mistake Amit Bijlani , or am I overlooking something?

9 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Good catch. We should have tested out that feature in the final stage but were too caught up in the teaching. A fix would be to change the tap gesture to a long press gesture. I've tested it and it works. Here are the modified project files for the Extra Credit

Thanks Amit! awesome support! I was not expecting this until sometime next week.

I see how the long press would avoid the conflict all together. But if we wanted to use a double-tap instead of a long press how would we go about doing that?

Hi Thomas,

The methods that are called should be

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = [[UIImageView alloc] init];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(like)];
        tap.numberOfTapsRequired = 2;
        [self addGestureRecognizer:tap];


        [self.contentView addSubview:self.imageView];
    }
    return self;
}


//and


-(void)like {

    NSLog(@"Like: %@",self.photo[@"link"]);

    NSURLSession *session = [NSURLSession sharedSession];
    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    NSString *urlstring = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/media/%@/likes?access_token=%@",self.photo[@"id"], accessToken];

    NSURL *url = [[NSURL alloc]initWithString:urlstring];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    request.HTTPMethod = @"POST";
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        dispatch_async(dispatch_get_main_queue(), ^{
            [self showLikeCompeletion];
        });

    }];
    [task resume];

}

-(void)showLikeCompeletion {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Liked!" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [alert show];

    double delayInSecounds = 1.0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSecounds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    });
}

I hope this helps.

Thomas Nilsen
Thomas Nilsen
14,957 Points

I know that :) But when you try and double tap an image. It gets displayed right away because of the delegate. I downloaded the final project and it's not working there either

What is listed for the delayInSecounds? is = 0?

Try changing it and see what happens. I'll download the project and see what happens too.

Thomas Nilsen
Thomas Nilsen
14,957 Points

No change. remember to download the final project btw.

The one under extra credit?

The one under extra credit?

Yeah it's not working for me.. :(

Thomas Nilsen
Thomas Nilsen
14,957 Points

Kind of glad it wasn't just me ;) I guess we'll have to wait and see what Amit says :)

It's kinda cool to see what the end product looks like. I'm just finishing up Refactoring Networking Code. This is been one of the better iOS projects on Treehouse!

Thomas Nilsen
Thomas Nilsen
14,957 Points

I know! I've already been through it twice :) I'm trying to use everything I've learned here in my app. Main difference being using the Foursquare API instead :)

That is awesome!

Thomas Nilsen
Thomas Nilsen
14,957 Points

Chris Gamio One way would be to not use the collectionview delegate that handles taps, and use only gesture-recognizers