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

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,好了,结束。。。



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