iOS 点击图片放大效果

这次带来的是点击图片放大效果,(就是手机最开始是照片的略缩图,点击后放大成正常图片)

因为项目要求用户上传的照片要像在本地一样,开始是略缩图显示,点击后进入详情并放大,所以这次分享下这个功能简单的实现原理,话不多说,上代码.

viewContoller的. m 里的代码如下:

#import "ViewController.h"

#import "PhotoViewController.h"


@interface ViewController ()

@property (nonatomic, retain)NSMutableArray *array;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

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

    

    self.array = [NSMutableArray arrayWithObjects:@"1155.jpg", @"1166.jpg"@"1177.jpg", nil];

    

    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 300, 200)];

    img.image = [UIImage imageNamed:[self.array objectAtIndex:0]];

    

    img.userInteractionEnabled = YES;

    

    //截掉边框

    img.clipsToBounds = YES;

    

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

    

    [img addGestureRecognizer:tap];

    

    [self.view addSubview:img];

    

    

    

    

    

    

    

}

- (void)tapAction

{

    PhotoViewController *photoVC = [[PhotoViewController alloc] init];

    

    photoVC.photoArr = self.array;


    

    [photoVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

    

    [self presentModalViewController:photoVC animated:YES];

    

}


之后我们还需要建立一个实现照片放大,缩小的 controller

.h 里需要声明一个存放照片的数组


#import <UIKit/UIKit.h>


@interface PhotoViewController : UIViewController

@property (nonatomic, retain)NSMutableArray *photoArr;

@end


.m里:

#import "PhotoViewController.h"


@interface PhotoViewController ()


@end


@implementation PhotoViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];

    

    myScrollView.backgroundColor = [UIColor blackColor];

    myScrollView.pagingEnabled = YES;

    myScrollView.bounces = NO;

    

    [self.view addSubview:myScrollView];

    

    myScrollView.contentSize = CGSizeMake(375 * self.photoArr.count, 667);

    

    for (int i = 0; i < self.photoArr.count; i++)

    {

        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(375 * i + 10, 0, 355, 667)];

        NSString *imgName = self.photoArr[i];

        img.image = [UIImage imageNamed:imgName];

        

        [myScrollView addSubview:img];

        

        //自适应图片大小

        img.contentMode = UIViewContentModeScaleAspectFit;

        

        

    }

    

    //轻拍跳出照片浏览

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

    

    [myScrollView addGestureRecognizer:tap];

    

    

    

这样就实现了点击图片放大的效果    

    

    

    

    

    

    

}

- (void)tapAction

{

    [self dismissViewControllerAnimated:YES completion:^{

        

        

    }];

}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值