因为项目要求用户上传的照片要像在本地一样,开始是略缩图显示,点击后进入详情并放大,所以这次分享下这个功能简单的实现原理,话不多说,上代码.
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
@interface PhotoViewController : UIViewController
@property (nonatomic, retain)NSMutableArray *photoArr;
@end
#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:^{
}];
}