点击图片放大查看

当我们查看一些小图片时,我们有时会看不清,我们就想把图片放大来看,下面的这些代码就简单实现了此功能

//视图控制器
#import "RootViewController.h"
#import "ClickZoomImageView.h"//图片类

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    ClickZoomImageView *imageView = [[ClickZoomImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.clickable = YES;
    imageView.image = [UIImage imageNamed:@"test.png"];
    [self.view addSubview:imageView];
    [imageView release];
}

#import <UIKit/UIKit.h>

@interface ClickZoomImageView : UIImageView

@property (nonatomic, assign) BOOL clickable;

@end
#import "ClickZoomImageView.h"
#define kScreenBounds [[UIScreen mainScreen] bounds]

@interface ClickZoomImageView ()

@property (nonatomic, retain) UIView *bgView;
@end

@implementation ClickZoomImageView

- (void)setClickable:(BOOL)clickable {
    if (clickable == NO) return ;

    _clickable = clickable;
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnImageView:)];
    [self addGestureRecognizer:tap];
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    NSLog(@"%f %f", self.frame.size.width, self.frame.size.height);
}

- (void)tapOnImageView:(UITapGestureRecognizer *)tap {
    // 第一步,取到bgView[上面已经放了一个imageView(我们要放大和缩小的imageView)]

    // 第二步:取到bgView上的imageView

    // 第三步:放大
    UIImageView *imageView = (id)[self.bgView viewWithTag:100];
    CGRect frame           = imageView.frame;
    CGFloat height         = frame.size.width/kScreenBounds.size.width*kScreenBounds.size.height;
    CGFloat width          = kScreenBounds.size.width;
    frame.size.width       = width;
    frame.size.height      = height;
    [UIView animateWithDuration:0.25 animations:^{
    imageView.frame        = frame;
    imageView.center       = _bgView.center;
    } completion:^(BOOL finished) {
        UITapGestureRecognizer *tapOnBGView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnBGView:)];
        [_bgView addGestureRecognizer:tapOnBGView];
    }];
}

- (void)tapOnBGView:(id)sender {
    CGRect rect = [self convertRect:self.bounds toView:[[UIApplication sharedApplication] keyWindow]];
    UIImageView *targetImageView = (id)[_bgView viewWithTag:100];
    [UIView animateWithDuration:0.25 animations:^{
        targetImageView.frame = rect;
    } completion:^(BOOL finished) {
        [_bgView removeFromSuperview];
        self.bgView = nil;
    }];
}

// 一旦调用bgView的getter方法的时候,就会触发这个方法
- (UIView *)bgView {
    if (_bgView == nil) {
        //  让_bgView和屏幕一样大
        _bgView = [[UIView alloc] initWithFrame:kScreenBounds];
        _bgView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.8];
        [[[UIApplication sharedApplication] keyWindow] addSubview:_bgView];

        // 坐标转换, 把自己的坐标转换成相对于window的坐标
        CGRect rect = [self convertRect:self.bounds toView:[[UIApplication sharedApplication] keyWindow]];
        // 这个targetImageView就是要放大和缩小的imageView
        UIImageView *targetImageView = [[UIImageView alloc] initWithFrame:rect];
        targetImageView.tag          = 100;
        targetImageView.image        = self.image;
        [_bgView addSubview:targetImageView];
    }
    return _bgView;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值