Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make an image expand to full screen when touched?

Tags:

ios

uiimage

I'd like to make a Facebook-like iOS app, which makes a picture from the timeline full screen when the user taps on it.

Update:

I'm using UICollectionView to display image in cell, so seems I should using collectionView:didSelectItemAtIndexPath: method? and the imageView is in the cell, so can I still expand to full screen directly?

Attached a couple images below:

enter image description here

enter image description here

like image 779
Seraph J. Lin Avatar asked Jul 19 '13 11:07

Seraph J. Lin


People also ask

How do I make an image fit my screen in HTML?

If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element.


1 Answers

Here's a fairly basic solution. It assumes that your collection cell has a UIImageView as the only subview of the UICollectionViewCell contentView.

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

...

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [collectionView 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];
                     }];
}
like image 104
TomSwift Avatar answered Sep 18 '22 12:09

TomSwift