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

需求

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

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

你可能感兴趣的:(点击图片,转换坐标系,放大图片)