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:
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.
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];
}];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With