ios点击头像放大并能伸缩拖动

点击头像放大,其实原理就是在头像处创建一个和头像一样大小的scrollview,然后让其放大至屏幕大小,点击屏幕取消时将其frame恢复之前大小,然后让其消失。

1.首先创建头像和用来创建点击手势的视图

            AsyncImageView *touxiang=[[AsyncImageView alloc]init];
            touxiang.isAcceptTouch=YES;
            touxiang.tag=7758521;
            touxiang.frame=CGRectMake(SCREEN_WIDTH-80, 10, 40, 40);
            [touxiang setDefaultImageNameString:@"用户默认头像.png"];//默认图片
            [touxiang setFailLoadImageNameString:@"用户默认头像.png"];//加载失败的图片
            [touxiang loadImageFromURLString:[UtilityHelper getPicUrlByWidthAndHeight:@"" @"60" height:@"60"]];//图片的URL
            [self.contentView addSubview:touxiang];
            

            //加手势
            UIView *tapView=[[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-80, 10, 40, 40)];
            tapView.tag=7758520;
            tapView.backgroundColor=[UIColor clearColor];
            [self.contentView addSubview:tapView];
            UITapGestureRecognizer *tapG=[[UITapGestureRecognizer alloc]initWithTarget:(SouFunMyCountController *)parentCtr action:@selector(fangdaTx:)];
            tapG.delegate=(SouFunMyCountController *)parentCtr;
            [tapView addGestureRecognizer:tapG];

2.在接收方法中处理图片

//放大头像
-(void)fangdaTx:(UITapGestureRecognizer *)sender
{
    CGPoint SenderPoint = [sender locationInView:self.myCountTable];
    NSIndexPath * indexPath =[self.myCountTable.utilityTableView indexPathForRowAtPoint:SenderPoint];
    UITableViewCell * cell = [self.myCountTable.utilityTableView cellForRowAtIndexPath:indexPath];
    UIView * imageview = (UIView *)[self.myCountTable viewWithTag:7758520];
    AsyncImageView * asycimage = (AsyncImageView*)[self.myCountTable viewWithTag:7758521];
    

    //获取最开始头像位置
    self.originalFrame = CGRectMake(cell.frame.origin.x+imageview.frame.origin.x, cell.frame.origin.y+imageview.frame.origin.y-self.myCountTable.utilityTableView.contentOffset.y+64, imageview.frame.size.width, imageview.frame.size.height);
    
    
    self.imgHead=asycimage.image;
    
    if (self.scrollView==nil) {
        UIScrollView *sc=[[UIScrollView alloc]initWithFrame:self.originalFrame];
        sc.delegate=self;
        sc.contentSize=self.originalFrame.size;
        sc.backgroundColor=[UIColor blackColor];
        sc.maximumZoomScale=2;
        sc.minimumZoomScale=0.5;
        self.scrollView=sc;
        [self.view.window addSubview:self.scrollView];
    }
    
    UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelTx)];
    [self.scrollView addGestureRecognizer:gesture];
    
    if (self.imgViewHead==nil) {
        UIImageView *imgV=[[UIImageView alloc]initWithImage:self.imgHead];
        imgV.frame=CGRectMake(0, 0, self.originalFrame.size.width, self.originalFrame.size.height);
        self.imgViewHead=imgV;
        [self.scrollView addSubview:self.imgViewHead];
    }
    
    [UIView animateWithDuration:0.2 animations:^{          //放大frame
        self.scrollView.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.imgViewHead.frame=CGRectMake((SCREEN_WIDTH-self.imgHead.size.width)/2, (SCREEN_HEIGHT-self.imgHead.size.height)/2, self.imgHead.size.width, self.imgHead.size.height);
      
        self.scrollView.contentSize=self.imgHead.size;
    } completion:^(BOOL finished) {
        
    }];
}

3.点击图片取消放大效果

//取消头像弹窗
-(void)cancelTx
{
    [UIView animateWithDuration:0.2 animations:^{
        self.scrollView.frame=self.originalFrame;
        self.imgViewHead.frame=CGRectMake(0, 0, self.originalFrame.size.width, self.originalFrame.size.height);
        self.scrollView.contentSize=self.imgHead.size;
    } completion:^(BOOL finished) {
        [self.imgViewHead removeFromSuperview];
        [self.scrollView removeFromSuperview];
        self.imgViewHead=nil;
        self.scrollView=nil;
    }];
}

4.uiscrollview代理方法,用来处理缩放

#pragma mark--scrollviewdelegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imgViewHead;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    self.imgViewHead.center=self.scrollView.center;
}

原文地址:https://www.cnblogs.com/zhangkk/p/4633818.html