iOS - 关于点击小图片之后查看对应大图的实现
2016-10-20 16:57
429 查看
在此,封装了一个类,外面用的话直接调用方法,即可实现想要的效果。但是有一点,点进去之后只能查看对应的一张图,不能滑动,如果想要更多的效果,自己在此基础上进行再封装吧。只做参考。代码如下:
.h文件中:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface SJAvatarBrowser :
NSObject
+(void)showImage:(UIImageView *)avatarImageView;
.m文件中:
#import "SJAvatarBrowser.h"
static CGRect oldframe;
@implementation SJAvatarBrowser
+(void)showImage:(UIImageView *)avatarImageView{
UIImage *image=avatarImageView.image;
UIWindow *window=[UIApplication
sharedApplication].keyWindow;
UIView *backgroundView=[[UIView
alloc]initWithFrame:CGRectMake(0,
0, [UIScreen
mainScreen].bounds.size.width, [UIScreen
mainScreen].bounds.size.height)];
oldframe=[avatarImageView
convertRect:avatarImageView.bounds
toView:window];
backgroundView.backgroundColor=[UIColor
blackColor];
backgroundView.alpha=0;
UIImageView *imageView=[[UIImageView
alloc]initWithFrame:oldframe];
imageView.image=image;
imageView.tag=1;
[backgroundView addSubview:imageView];
[window addSubview:backgroundView];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(hideImage:)];
[backgroundView addGestureRecognizer: tap];
[UIView
animateWithDuration:0.3
animations:^{
imageView.frame=CGRectMake(0,([UIScreen
mainScreen].bounds.size.height-image.size.height*[UIScreen
mainScreen].bounds.size.width/image.size.width)/2,
[UIScreen mainScreen].bounds.size.width,
image.size.height*[UIScreen
mainScreen].bounds.size.width/image.size.width);
backgroundView.alpha=1;
} completion:^(BOOL finished) {
}];
}
+(void)hideImage:(UITapGestureRecognizer*)tap{
UIView *backgroundView=tap.view;
UIImageView *imageView=(UIImageView*)[tap.view
viewWithTag:1];
[UIView
animateWithDuration:0.3
animations:^{
imageView.frame=oldframe;
backgroundView.alpha=0;
} completion:^(BOOL finished) {
[backgroundView removeFromSuperview];
}];
}
////////////////////////////////
在此,一个封装好的类,已经结束。下面以ViewController为例,来说明:
#import "ViewController.h"
#import "SJAvatarBrowser.h"
@interface ViewController () {
UIImageView *_imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageView = [[UIImageView
alloc] init];
imageView.frame =
CGRectMake(100,
100, 300,
300);
imageView.image = [UIImage
imageNamed:@"4.jpg"];
imageView.userInteractionEnabled =
YES;
[self.view
addSubview:imageView];
_imageView = imageView;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(magnifyImage:)];
[imageView addGestureRecognizer:tap];
}
- (void)magnifyImage:(UITapGestureRecognizer *)gesture {
NSLog(@"版权所有,违者必究,Q_Q33757152的博客");
[SJAvatarBrowser
showImage:_imageView];//调用方法
}
最后,一切OK,好了,结束。。。
.h文件中:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface SJAvatarBrowser :
NSObject
+(void)showImage:(UIImageView *)avatarImageView;
.m文件中:
#import "SJAvatarBrowser.h"
static CGRect oldframe;
@implementation SJAvatarBrowser
+(void)showImage:(UIImageView *)avatarImageView{
UIImage *image=avatarImageView.image;
UIWindow *window=[UIApplication
sharedApplication].keyWindow;
UIView *backgroundView=[[UIView
alloc]initWithFrame:CGRectMake(0,
0, [UIScreen
mainScreen].bounds.size.width, [UIScreen
mainScreen].bounds.size.height)];
oldframe=[avatarImageView
convertRect:avatarImageView.bounds
toView:window];
backgroundView.backgroundColor=[UIColor
blackColor];
backgroundView.alpha=0;
UIImageView *imageView=[[UIImageView
alloc]initWithFrame:oldframe];
imageView.image=image;
imageView.tag=1;
[backgroundView addSubview:imageView];
[window addSubview:backgroundView];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(hideImage:)];
[backgroundView addGestureRecognizer: tap];
[UIView
animateWithDuration:0.3
animations:^{
imageView.frame=CGRectMake(0,([UIScreen
mainScreen].bounds.size.height-image.size.height*[UIScreen
mainScreen].bounds.size.width/image.size.width)/2,
[UIScreen mainScreen].bounds.size.width,
image.size.height*[UIScreen
mainScreen].bounds.size.width/image.size.width);
backgroundView.alpha=1;
} completion:^(BOOL finished) {
}];
}
+(void)hideImage:(UITapGestureRecognizer*)tap{
UIView *backgroundView=tap.view;
UIImageView *imageView=(UIImageView*)[tap.view
viewWithTag:1];
[UIView
animateWithDuration:0.3
animations:^{
imageView.frame=oldframe;
backgroundView.alpha=0;
} completion:^(BOOL finished) {
[backgroundView removeFromSuperview];
}];
}
////////////////////////////////
在此,一个封装好的类,已经结束。下面以ViewController为例,来说明:
#import "ViewController.h"
#import "SJAvatarBrowser.h"
@interface ViewController () {
UIImageView *_imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageView = [[UIImageView
alloc] init];
imageView.frame =
CGRectMake(100,
100, 300,
300);
imageView.image = [UIImage
imageNamed:@"4.jpg"];
imageView.userInteractionEnabled =
YES;
[self.view
addSubview:imageView];
_imageView = imageView;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(magnifyImage:)];
[imageView addGestureRecognizer:tap];
}
- (void)magnifyImage:(UITapGestureRecognizer *)gesture {
NSLog(@"版权所有,违者必究,Q_Q33757152的博客");
[SJAvatarBrowser
showImage:_imageView];//调用方法
}
最后,一切OK,好了,结束。。。
相关文章推荐
- [置顶] Android 实现WebView点击图片查看大图列表及图片保存
- iOS-获取UIWebView或者WKWebView页面的左右图片链接,点击查看大图
- Android 实现WebView点击图片查看大图列表及图片保存
- jquery实现移动端点击图片查看大图特效
- 基于JavaScript实现移动端点击图片查看大图点击大图隐藏
- 点击发送后图片查看大图的实现思路
- Android 利用JS 实现对网络图片添加点击事件 查看大图
- iOS-获取UIWebView或者WKWebView页面的左右图片链接,点击查看大图
- Android 实现WebView点击图片查看大图列表及图片保存功能
- IOS 点击查看预览大图缩放图片往下掉的处理
- iOS - 关于点击小图查看大图的封装(包含单击、双击、捏合手势)
- jquery实现移动端点击图片查看大图特效
- 基于JavaScript实现移动端点击图片查看大图点击大图隐藏
- iOS -点击按钮查看大图,再次点击还原效果(此处以UITableViewCell为父视图,其中加载多个图片为例)
- js写的方法实现上传图片之后查看大图
- 如何用Jquery实现 ,比如点击图片之后 ,该图片变成向下的箭头,再点击向下箭头的图片 又变成原始图片呢
- IOS关于从照相机获取图片的问题,初步探究通过摄像头实现颜色与形状识别机制==
- ImageView和onTouchListener实现,点击查看图片细节
- UIWebview点击图片实现大图浏览