iOS -点击按钮查看大图,再次点击还原效果(此处以UITableViewCell为父视图,其中加载多个图片为例)

#import "xxxCell.h"

#import <UIButton+WebCache.h>

staticCGRect oldframe;//用于记录放大之前的frame

@implementation xxxCell


- (void)awakeFromNib {


}


//1.给cell赋值

-(void)reDrawInfoContentView:(xxxModel*)model{


   CGRect aRect, bRect, bounds = CGRectMake(10, height + 25, self.contentView.bounds.size.width,10000000);

   NSInteger rows = model.pictures.count%3==0?model.pictures.count/3:model.pictures.count/3+1;

   for (int i =0; i<rows; i++) {

       CGRectDivide(bounds, &aRect, &bounds, 90, CGRectMinYEdge);

       for (int m =0; m<3; m++) {

           NSInteger index = i * 3 + m;

           if (index >= model.pictures.count) {

               return;

            }

           CGRectDivide(aRect, &bRect, &aRect, bounds.size.width/3,CGRectMinXEdge);

            

           UploadFileDto *fileDto = [model.picturesobjectAtIndex:index];

            UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

            button.frame = bRect;

            [buttonsd_setImageWithURL:[NSURLURLWithString:fileDto.url]forState:UIControlStateNormal];

            [button addTarget:selfaction:@selector(showImage:)forControlEvents:UIControlEventTouchUpInside];

            [self.contentViewaddSubview:button];

        }

    }

}

//2.放大图片

-(void)showImage:(UIButton *)button{

    UIWindow *window=[UIApplicationsharedApplication].keyWindow;

    UIView *backgroundView=[[UIViewalloc]initWithFrame:CGRectMake(0,0, [UIScreenmainScreen].bounds.size.width, [UIScreenmainScreen].bounds.size.height)];

   oldframe=[button convertRect:button.boundstoView:self.contentView];

    backgroundView.backgroundColor=[UIColorblackColor];

    backgroundView.alpha=0;

    button.tag=1;

    [backgroundViewaddSubview:button];

    [windowaddSubview:backgroundView];

    

    UITapGestureRecognizer *tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(hideImage:)];

    [backgroundViewaddGestureRecognizer:tap];

    

    [UIViewanimateWithDuration:0.3animations:^{

        button.frame=CGRectMake(0,([UIScreenmainScreen].bounds.size.height-(button.frame.size.height*[UIScreenmainScreen].bounds.size.width/button.frame.size.width+80))/2, [UIScreen mainScreen].bounds.size.width, button.frame.size.height*[UIScreenmainScreen].bounds.size.width/button.frame.size.width+80);

        backgroundView.alpha=1;

    }completion:^(BOOL finished) {

        button.userInteractionEnabled=NO;

    }];

}

//3.还原图片

-(void)hideImage:(UITapGestureRecognizer*)tap{

   UIView *backgroundView=tap.view;

   UIButton *button=(UIButton *)[tap.viewviewWithTag:1];

    [UIViewanimateWithDuration:0.3animations:^{

        button.frame=oldframe;

        backgroundView.alpha=0;

    }completion:^(BOOL finished) {

        [backgroundViewremoveFromSuperview];

        [self.contentViewaddSubview:button];

        button.userInteractionEnabled=YES;

    }];

}

@end


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值