iOS开发——点击图片全屏显示
2016-04-25 17:10
477 查看
点击图片,全屏显示,然后再点击屏幕一下,返回。
没啥难的,直接上代码:
//
// ViewController.m
// Demo-hehe
//
// Created by yyt on 16/4/25.
// Copyright © 2016年 yyt. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, self.view.bounds.size.width-40, 300)];
imageView.image = [UIImage imageNamed:@"11"];
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)];
tapGesture.numberOfTapsRequired=1;
[imageView addGestureRecognizer:tapGesture];
}
//缩放图片
-(void)showZoomImageView:(UITapGestureRecognizer *)tap
{
if (![(UIImageView *)tap.view image]) {
return;
}
UIView *bgView = [[UIView alloc] init];
bgView.frame = [UIScreen mainScreen].bounds;
bgView.backgroundColor = [UIColor blackColor];
[[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
UITapGestureRecognizer *tapBgView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
[bgView addGestureRecognizer:tapBgView];
//必不可少的一步,如果直接把点击获取的imageView拿来玩的话,返回的时候,原图片就完蛋了
UIImageView *tempImageView = (UIImageView*)tap.view;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:tempImageView.frame];
imageView.image = tempImageView.image;
[bgView addSubview:imageView];
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = imageView.frame;
frame.size.width = bgView.frame.size.width;
frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
frame.origin.x = 0;
frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
imageView.frame = frame;
}];
}
-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
[tapBgRecognizer.view removeFromSuperview];
}
@end
没啥难的,直接上代码:
//
// ViewController.m
// Demo-hehe
//
// Created by yyt on 16/4/25.
// Copyright © 2016年 yyt. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, self.view.bounds.size.width-40, 300)];
imageView.image = [UIImage imageNamed:@"11"];
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)];
tapGesture.numberOfTapsRequired=1;
[imageView addGestureRecognizer:tapGesture];
}
//缩放图片
-(void)showZoomImageView:(UITapGestureRecognizer *)tap
{
if (![(UIImageView *)tap.view image]) {
return;
}
UIView *bgView = [[UIView alloc] init];
bgView.frame = [UIScreen mainScreen].bounds;
bgView.backgroundColor = [UIColor blackColor];
[[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
UITapGestureRecognizer *tapBgView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
[bgView addGestureRecognizer:tapBgView];
//必不可少的一步,如果直接把点击获取的imageView拿来玩的话,返回的时候,原图片就完蛋了
UIImageView *tempImageView = (UIImageView*)tap.view;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:tempImageView.frame];
imageView.image = tempImageView.image;
[bgView addSubview:imageView];
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = imageView.frame;
frame.size.width = bgView.frame.size.width;
frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
frame.origin.x = 0;
frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
imageView.frame = frame;
}];
}
-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
[tapBgRecognizer.view removeFromSuperview];
}
@end
相关文章推荐
- iOS自定义键盘切换效果
- iOS-开发之 pch 文件的使用
- iOS 打包ipa测试包
- iOS开发——缩放图片
- audio 在ios上自动播放问题
- 【转载】iOS提示框,为什么你应该使用 MBProgressHUD?
- iOS-仿网易云音乐控制器
- iOS 邮箱、手机号、身份证....正则表达式
- iOS-仿百度卫士动画
- iOS 模态弹出 presentViewController弹出页面时有延迟
- iOS如何上传代码到Github
- iOS Core Animation
- IOS开源项目学习---oschina
- iOS代码对齐 ---XAlign
- iOS屏幕旋转 浅析
- IOS中延时执行的几种方式的比较和汇总
- IOS开发之----使用ZipArchive解压缩文件
- NSNotification-iOS初学
- iOS每日一记之-------------------获取label的行数。。。。。恩
- 关于iOS定位问题