ios - 触摸时如何使图像扩展到全屏?

标签 ios uiimage

我想制作一个类似 Facebook 的 iOS 应用程序,当用户点击它时,它会从时间轴全屏显示图片。

更新:

我正在使用 UICollectionView 在单元格中显示图像, 所以我似乎应该使用 collectionView:didSelectItemAtIndexPath: 方法? 并且imageView在cell中,我还能直接展开全屏吗?

下面附上几张图片:

enter image description here

enter image description here

最佳答案

这是一个相当基本的解决方案。它假设您的集合单元有一个 UIImageView 作为 UICollectionViewCell contentView 的唯一 subview 。

#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];
                     }];
}

关于ios - 触摸时如何使图像扩展到全屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17744689/

相关文章:

ios - 将Facebook个人资料图片放入UIImage

ios - 使用类似操作的演示 iOS 13

iphone - 如何创建一组 uiimages 的副本?

iphone - 保存没有质量损失的 UIImage

ios - 如何从iphone中的png文件中检索单个图像

iPhone 为图像添加模糊效果

ios - SwiftUI 在 TabView 中禁用特定的 tabItem 选择?

iOS 键盘扩展(应用组)

ios - 以编程方式访问 UIScrollView 的 subview

iphone - 如何在 iOS 中为 UIImage 像素化/添加像素化效果