Friday, 22 January 2016

UITableview tapping on UIImageView

#import <objc/runtime.h> // for objc_setAssociatedObject / objc_getAssociatedObject


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView cellForItemAtIndexPath:indexPath];

    UIImageView* iv = cell.contentView.subviews.lastObject;

    UIImageView* ivExpand = [[UIImageView alloc] initWithImage: iv.image];
    ivExpand.contentMode = iv.contentMode;
    ivExpand.frame = [self.view convertRect: iv.frame fromView: iv.superview];
    ivExpand.userInteractionEnabled = YES;
    ivExpand.clipsToBounds = YES;

    objc_setAssociatedObject( ivExpand,
                              "original_frame",
                              [NSValue valueWithCGRect: ivExpand.frame],
                              OBJC_ASSOCIATION_RETAIN);

    [UIView transitionWithView: self.view
                      duration: 1.0
                       options: UIViewAnimationOptionAllowAnimatedContent
                    animations:^{

                        [self.view addSubview: ivExpand];
                        ivExpand.frame = self.view.bounds;

                    } completion:^(BOOL finished) {

                        UITapGestureRecognizer* tgr = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector( onTap: )];
                        [ivExpand addGestureRecognizer: tgr];
                    }];
}

- (void) onTap: (UITapGestureRecognizer*) tgr
{
    [UIView animateWithDuration: 1.0
                     animations:^{

                         tgr.view.frame = [objc_getAssociatedObject( tgr.view,
                                                                    "original_frame" ) CGRectValue];
                     } completion:^(BOOL finished) {

                         [tgr.view removeFromSuperview];
                     }];
}

No comments:

Post a Comment