ios开发日常 - 点击头像放大图片

示例图:

点击放大图.gif
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *lastImageView;
@property (nonatomic, assign)CGRect originalFrame;


-(void)showZoomImageView
{
    //scrollView作为背景
    UIScrollView *bgView = [[UIScrollView alloc] init];
    bgView.frame = [UIScreen mainScreen].bounds;
    bgView.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
    [bgView addGestureRecognizer:tapBg];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    NSURL *imgUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@/property/%@",BASEURL,[signModel headimgUrl]]];
    [imageView sd_setImageWithURL:imgUrl placeholderImage:[UIImage imageNamed:@"taskOwner"]];

//    imageView.frame = [bgView convertRect:picView.frame fromView:self.view];
    imageView.frame = CGRectMake(kScreen_Width - 80, 130, 0, 0);  //设置原始位置
    [bgView addSubview:imageView];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
    
    self.lastImageView = imageView;
    self.originalFrame = imageView.frame;
    self.scrollView = bgView;
    //最大放大比例
    self.scrollView.maximumZoomScale = 1.5;
    self.scrollView.delegate = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = imageView.frame;
        frame.size.width = bgView.frame.size.width;
        frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
        frame.origin.x = 0;
        frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
        imageView.frame = frame;
    }];
}

-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
    self.scrollView.contentOffset = CGPointZero;
    [UIView animateWithDuration:0.5 animations:^{
        self.lastImageView.frame = self.originalFrame;
        tapBgRecognizer.view.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [tapBgRecognizer.view removeFromSuperview];
        self.scrollView = nil;
        self.lastImageView = nil;
    }];
}

#pragma ScrollView //返回可缩放的视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.lastImageView;
}

参考脚步之家网站的hebedich
http://www.jb51.net/article/74846.htm

你可能感兴趣的:(ios开发日常 - 点击头像放大图片)