点击手势识别器是一个轻拍

时间:2014-04-24 23:48:02

标签: ios objective-c uicollectionview uitapgesturerecognizer

我有collectionView张照片,当点按照片时,它应显示该照片的详细视图。

问题是,我点击的第一张照片没有做任何事情,然后每次点按后都会工作,除了它显示两次前拍摄的照片。

例如:

点击照片1 ...什么都不做。

点击照片2 ...显示照片1。

点击照片6 ...显示照片2。

感谢您的帮助!

ViewController.m

@property (nonatomic) NSArray *photos;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[BlissPhotoCell class] 
    forCellWithReuseIdentifier:@"photo"];

    NSURLSession *session = [NSURLSession sharedSession];
    NSString *urlString = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/;
    NSURL *url = [[NSURL alloc]initWithString:urlString];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
        NSString *text = [[NSString alloc] initWithContentsOfURL:location encoding:NSUTF8StringEncoding error:nil];

        NSData *data = [[NSData alloc] initWithContentsOfURL:location];
        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        self.photos = [responseDictionary valueForKeyPath:@"data"];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
    [task resume];

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *photo = self.photos[indexPath.row];
    BlissDetailViewController *viewController = [[BlissDetailViewController alloc] init];

    viewController.modalPresentationStyle = UIModalPresentationCustom;
    viewController.transitioningDelegate = self;

    viewController.photo = photo;
    [self presentViewController:viewController animated:YES completion:nil];
}

BlissPresentDetailTransition.h

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *detail = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];

    detail.view.alpha = 0.0;
    CGRect frame = containerView.bounds;
    frame.origin.y += 20.0;
    frame.size.height -= 20.0;
    detail.view.frame = frame;
    [containerView addSubview:detail.view];

    [UIView animateWithDuration:0.3 animations:^{
        detail.view.alpha = 1.0;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.3;
}

1 个答案:

答案 0 :(得分:4)

您无意中实施了didDeselect而不是didSelect。如果您在键入时使用自动完成,则很容易犯错,因为didDeselect首先按字母顺序排列。