征服iOS

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

2016-06-16  本文已影响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;
    }];
}
上一篇下一篇

猜你喜欢

热点阅读