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 trialThomas Nilsen
14,957 PointsBuild 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
Treehouse Guest TeacherGood 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
agreatdaytocode
24,757 PointsHi 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
14,957 PointsI 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
agreatdaytocode
24,757 PointsWhat is listed for the delayInSecounds? is = 0?
Thomas Nilsen
14,957 PointsdelayInSecounds = 1
agreatdaytocode
24,757 PointsTry changing it and see what happens. I'll download the project and see what happens too.
Thomas Nilsen
14,957 PointsNo change. remember to download the final project btw.
agreatdaytocode
24,757 PointsThe one under extra credit?
agreatdaytocode
24,757 PointsThe one under extra credit?
Thomas Nilsen
14,957 Pointsyes :)
agreatdaytocode
24,757 PointsYeah it's not working for me.. :(
Thomas Nilsen
14,957 PointsKind of glad it wasn't just me ;) I guess we'll have to wait and see what Amit says :)
agreatdaytocode
24,757 PointsIt'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
14,957 PointsI 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 :)
agreatdaytocode
24,757 PointsThat is awesome!
Thomas Nilsen
14,957 PointsChris Gamio One way would be to not use the collectionview delegate that handles taps, and use only gesture-recognizers
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsThanks Amit! awesome support! I was not expecting this until sometime next week.
Thomas Nilsen
14,957 PointsThomas Nilsen
14,957 PointsThanks a lot :)
Chris Gamio
3,358 PointsChris Gamio
3,358 PointsI 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?