您的位置:首页 > 移动开发 > IOS开发

iOS 点击图片放大效果

2016-05-17 10:35 447 查看

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

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

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:^{

        

        

    }];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: