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:^{
}];
}
相关文章推荐
- 欢迎使用CSDN-markdown编辑器
- 整理iOS工作学习过程中遇到的各种问题
- AVSpeechSynthesizer详解
- iOS横竖屏转换问题
- iOS开发-常用第三方开源框架介绍
- iOS性能优化:Instruments使用实战
- iOS第三方音频框架TheAmazingAudioEngine使用及音效实现介绍
- Mac/ios 模拟器 测试模拟慢网速
- iOS开发之音频解析第三方框架介绍
- iOS获取通讯录联系人信息
- 关于ios使用LaunchScreen作为欢迎页图片加载不上的问题
- CoreData初次使用
- iOS 应用性能测试的相关方法、工具及技巧
- NSURLSession 初探
- iOS应用内跳转
- 墙裂推荐 iOS 资源大全
- iOS 理解KVO与KVC
- iOS强制横屏
- iOS类型里面添加属性
- iOS-本地换肤思路