美文网首页征服iOS
iOS 类似微博点击图片放大缩小

iOS 类似微博点击图片放大缩小

作者: 童雨 | 来源:发表于2016-06-16 16:59 被阅读0次

    _cellImage=[UIImage imageWithData:EncryptData];
    [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]];
    imageView.userInteractionEnabled = YES;

    - (void)tapImageView:(UITapGestureRecognizer *)recognizer
    {
        
        //添加遮盖
        UIView *cover = [[UIView alloc] init];
        cover.frame = [UIScreen mainScreen].bounds;
        cover.backgroundColor = [UIColor clearColor];
        [cover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCover:)]];
        [[UIApplication sharedApplication].keyWindow addSubview:cover];
        
        //添加图片到遮盖上
        UIImageView *imageView = [[UIImageView alloc] initWithImage:_cellImage];
        imageView.frame = [cover convertRect:recognizer.view.frame fromView:self];
        self.lastFrame = imageView.frame;
        [cover addSubview:imageView];
        self.imagView = imageView;
        
        //放大
        [UIView animateWithDuration:0.3f animations:^{
            cover.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8];
            CGRect frame = imageView.frame;
            frame.size.width = cover.frame.size.width;
            frame.size.height = cover.frame.size.width * (imageView.image.size.height / imageView.image.size.width);
            frame.origin.x = 0;
            frame.origin.y = (cover.frame.size.height - frame.size.height) * 0.5;
            imageView.frame = frame;
        }];
    }
    
    - (void)tapCover:(UITapGestureRecognizer *)recognizer
    {
        [UIView animateWithDuration:0.3f animations:^{
            recognizer.view.backgroundColor = [UIColor clearColor];
            self.imagView.frame = self.lastFrame;
            
        }completion:^(BOOL finished) {
            [recognizer.view removeFromSuperview];
            self.imagView = nil;
        }];
    }

    相关文章

      网友评论

        本文标题:iOS 类似微博点击图片放大缩小

        本文链接:https://www.haomeiwen.com/subject/yuimdttx.html