点击图片,转换坐标系,放大图片

需求

1、点击图片放大,全屏显示
2、首先设置遮盖层cover,遮盖层添加到keyWindow上。然后额外创建一个imageView,代替原先的图片
3、把新创建的imageView坐标系转换到遮盖层cover上,同事进行放大处理

代码如下:

MDAblumCell *cell = (MDAblumCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //添加遮盖
    UIView *cover = [[UIView alloc] init];
    cover.frame = [UIScreen mainScreen].bounds;
    cover.backgroundColor = [UIColor clearColor];
    [cover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCover:)]];
    [[UIApplication sharedApplication].keyWindow addSubview:cover];
    
    //添加图片到遮盖上
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.albums[indexPath.item]]];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    // 转换坐标系(自己更改自己的坐标系)
    imageView.frame = [cover convertRect:cell.frame fromView:self.collectionView];
    self.lastFrame = imageView.frame;
    [cover addSubview:imageView];
    self.imagView = imageView;
    
    //放大
    [UIView animateWithDuration:0.3f animations:^{
        cover.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.7];
        CGRect frame = imageView.frame;
        frame.size.width = cover.frame.size.width;
        frame.size.height = cover.frame.size.width * (imageView.image.size.height / imageView.image.size.width);
        frame.origin.x = 0;
        frame.origin.y = (cover.frame.size.height - frame.size.height) * 0.5;
        imageView.frame = frame;
    }];

// 缩小还原
- (void)tapCover:(UITapGestureRecognizer *)recognizer
{
    [UIView animateWithDuration:0.3f animations:^{
        recognizer.view.backgroundColor = [UIColor clearColor];
        self.imagView.frame = self.lastFrame;
        
    }completion:^(BOOL finished) {
        [recognizer.view removeFromSuperview];
        self.imagView = nil;
    }];
}

注意:自己修改自己所要转换的坐标系。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,650评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,169评论 25 709
  • 阅读让自己更加优秀 Because You live 关于阅读 拿到一本书应该怎样去读呢?这是一个复杂的问题。为何...
    涂涂写写祝晚安阅读 3,034评论 0 1
  • 今天朋友圈应该被自家人刷屏了,因为,在大众点评上的梵音瑜伽,已经有9家店了,除了蓝堡旺座这两家老店之外,新馆双井的...
    小猪猪同学阅读 3,328评论 4 4
  • 匆匆忙忙的上半年走完了,我也开始执笔,回看在这半年时间里—— 我经历了什么,又改变了些什么。我总是喜欢折腾自己,就...
    微笑弧度0308阅读 1,535评论 0 0

友情链接更多精彩内容