添加点击手势放大图片

//点击图片可放大图片至全框口,点击图片可返回原状态
-(void)awakeFromNib
{
    inoutFeild.delegate = self;
    
    //九个UIImageView添加手势
    for (UIView *view in photoView.subviews)
    {
        view.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTap:)];
        [view addGestureRecognizer:tap];
    }
    //圆角
    faceImageView.layer.cornerRadius = 37;
    faceImageView.clipsToBounds = YES;
}

CGRect preRect;
-(void)myTap:(UITapGestureRecognizer *)tap
{
    //获取UIWindow对象
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    //将被点击的视图对应于父视图的坐标转化为toView中给点的坐标
    preRect = [photoView convertRect:tap.view.frame toView:window];
    UIImageView *img = (UIImageView *)tap.view;
    
    //获取被用户点击的图片的frame
    UIImageView *newImg = [[UIImageView alloc] initWithFrame:preRect];
    newImg.image = img.image;
    newImg.userInteractionEnabled = YES;
    [w addSubview:newImg];
    
    //动画变大
    [UIView animateWithDuration:3 animations:^{
        newImg.frame = CGRectMake(0, 0, 320, 480);
    }];
    
    
    UITapGestureRecognizer *resetTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resetView:)];
    [newImg addGestureRecognizer:resetTap];
    
}

-(void)resetView:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.5 animations:^{
        tap.view.frame = preRect;
    } completion:^(BOOL finished) {
        [tap.view removeFromSuperview];
    }];
}


你可能感兴趣的:(添加点击手势放大图片)