您的位置:首页 > 产品设计 > UI/UE

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

2016-01-10 18:14 681 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息