实现当点击一张图片的时候,图片会放大,再点击图片的背景的时候,图片会缩放回去

WZStatusPhotoView *iv = [[WZStatusPhotoView alloc] init];

 //添加监听事件

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)];

 [iv addGestureRecognizer:tap];


//当点击图片的触发以下的事件

- (void)imageTap:(UITapGestureRecognizer *)tap

{

    //添加蒙版

    UIView *cover = [[UIView alloc] init];

    cover.alpha = 0.0;

    cover.backgroundColor = [UIColor blackColor];

    cover.frame = [UIScreen mainScreen].bounds;

    //把蒙版添加到Window

    [[UIApplication sharedApplication].keyWindow addSubview:cover];

    // 给蒙板添加监听事件

    UITapGestureRecognizer *coverTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick:)];

    [cover addGestureRecognizer:coverTap];


    //添加点击的事件到蒙版上

    UIImageView *newIv = [[UIImageView alloc] init];

    // 通过手势获取到点击的图片

    WZStatusPhotoView *photoView =  (WZStatusPhotoView *)tap.view;

    // 从点击的imageview中取出对应的模型数据

    NSURL *url = [NSURL URLWithString:photoView.photo.bmiddle_pic];

    [newIv sd_setImageWithURL:url placeholderImage:[UIImage imageWithName:@"timeline_image_placeholder"]];

    // 坐标系准换, :photoView.frame的坐标系从self(配图容器)的左上角转换为cover的左上角

    newIv.frame = [self convertRect:photoView.frame toView:cover];

    // 记录当前点击viewframe

    self.lastFrame = newIv.frame;

    // 记录缩放的view

    self.customIV = newIv;

    

    [cover addSubview:newIv];

    

    // 缩放点击的图片

    [UIView animateWithDuration:0.25 animations:^{

        

        cover.alpha = 1.0;

        // 设置图片的宽高和位置

        CGRect tempFrame =  newIv.frame;

        tempFrame.size.width = cover.width;

        tempFrame.size.height = tempFrame.size.width * (newIv.image.size.height / newIv.image.size.width);

        tempFrame.origin.x = 0;

        tempFrame.origin.y = (cover.height - tempFrame.size.height) * 0.5;

        newIv.frame = tempFrame;

        

    }];

}


//点击的时候,让图片缩回

- (void)coverClick:(UITapGestureRecognizer *)coverTap

{

    UIView *view =  coverTap.view;

    // 缩小配图

    [UIView animateWithDuration:0.25 animations:^{

        self.customIV.frame = self.lastFrame;

        view.alpha = 0.0;

    } completion:^(BOOL finished) {

        [view removeFromSuperview];

        self.customIV = nil;

    }];

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值