首页 > 代码库 > 图片点击放大

图片点击放大

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1d9421 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #822e0e } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3c828c } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c32275 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81 } p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } p.p11 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } span.s1 { } span.s2 { font: 18.0px Menlo } span.s3 { font: 18.0px "PingFang SC" } span.s4 { color: #822e0e } span.s5 { color: #0435ff } span.s6 { color: #c32275 } span.s7 { color: #000000 } span.s8 { color: #6122ae } span.s9 { color: #539aa4 } span.s10 { color: #3d1d81 } span.s11 { font: 18.0px Menlo; color: #000000 } span.s12 { color: #703daa } span.s13 { color: #78492a } span.s14 { color: #c91b13 } span.s15 { font: 18.0px "PingFang SC"; color: #c91b13 } span.s16 { color: #294c50 } span.s17 { color: #1d9421 } span.s18 { font: 18.0px "PingFang SC"; color: #1d9421 }</style>

//

//  ViewController.m

//  图片点击放大

//

//  Created by xueshan on 17/1/19.

//  Copyright ? 2017 xueshan. All rights reserved.

//

 

#import "ViewController.h"

#import <QuartzCore/QuartzCore.h>

 

//设置放大后图片的宽高,为了省时间,偷了下懒,建议最好结合实际做下运算

#define BIG_IMG_WIDTH  325

#define BIG_IMG_HEIGHT 325

 

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIImageView *minImageView;

 

 

@end

 

@implementation ViewController{

    UIView *background;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    //允许用户交互

    _minImageView.userInteractionEnabled = YES;

    //添加点击手势

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];

    [_minImageView addGestureRecognizer:tapGesture];

}

//点击图片后的方法(即图片的放大全屏效果)

- (void) tapAction{

    //创建一个黑色背景

    //初始化一个用来当做背景的View。我这里为了省时间计算,宽高直接用的5s的尺寸

    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 600)];

    background = bgView;

    [bgView setBackgroundColor:[UIColor blackColor]];

    [self.view addSubview:bgView];

    

    //创建显示图像的视图

    //初始化要显示的图片内容的imageView(这里位置继续偷懒...没有计算)

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, BIG_IMG_WIDTH, BIG_IMG_HEIGHT)];

    //要显示的图片,即要放大的图片

    [imgView setImage:[UIImage imageNamed:@"企业证书账单"]];

    [bgView addSubview:imgView];

    

    imgView.userInteractionEnabled = YES;

    //添加点击手势(即点击图片后退出全屏)

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];

    [imgView addGestureRecognizer:tapGesture];

    

    [self shakeToShow:bgView];//放大过程中的动画

}

-(void)closeView{

    [background removeFromSuperview];

}

//放大过程中出现的缓慢动画

- (void) shakeToShow:(UIView*)aView{

    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];

    animation.duration = 0.5;

    NSMutableArray *values = [NSMutableArray array];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];

    animation.values = values;

    [aView.layer addAnimation:animation forKey:nil];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

图片点击放大