iOS - 关于点击小图片之后查看对应大图的实现

来源:互联网 发布:看电影赚钱的软件 编辑:程序博客网 时间:2024/05/02 07:51

      在此,封装了一个类,外面用的话直接调用方法,即可实现想要的效果。但是有一点,点进去之后只能查看对应的一张图,不能滑动,如果想要更多的效果,自己在此基础上进行再封装吧。只做参考。代码如下:

.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=[UIApplicationsharedApplication].keyWindow;

    UIView *backgroundView=[[UIViewalloc]initWithFrame:CGRectMake(0,0, [UIScreenmainScreen].bounds.size.width, [UIScreenmainScreen].bounds.size.height)];

    oldframe=[avatarImageViewconvertRect:avatarImageView.boundstoView:window];

    backgroundView.backgroundColor=[UIColorblackColor];

    backgroundView.alpha=0;

    UIImageView *imageView=[[UIImageViewalloc]initWithFrame:oldframe];

    imageView.image=image;

    imageView.tag=1;

    [backgroundView addSubview:imageView];

    [window addSubview:backgroundView];

    

    UITapGestureRecognizer *tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(hideImage:)];

    [backgroundView addGestureRecognizer: tap];

    

    [UIViewanimateWithDuration:0.3animations:^{

        imageView.frame=CGRectMake(0,([UIScreenmainScreen].bounds.size.height-image.size.height*[UIScreenmainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreenmainScreen].bounds.size.width/image.size.width);

        backgroundView.alpha=1;

    } completion:^(BOOL finished) {

        

    }];

}


+(void)hideImage:(UITapGestureRecognizer*)tap{

    UIView *backgroundView=tap.view;

    UIImageView *imageView=(UIImageView*)[tap.viewviewWithTag:1];

    [UIViewanimateWithDuration:0.3animations:^{

        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 = [[UIImageViewalloc] init];

    imageView.frame = CGRectMake(100,100, 300,300);

    imageView.image = [UIImageimageNamed:@"4.jpg"];

    imageView.userInteractionEnabled =YES;

    [self.viewaddSubview:imageView];

    _imageView = imageView;

    

    UITapGestureRecognizer *tap  = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(magnifyImage:)];

    

    [imageView addGestureRecognizer:tap];

    

}

- (void)magnifyImage:(UITapGestureRecognizer *)gesture {

   NSLog(@"版权所有,违者必究,Q_Q33757152的博客");

    [SJAvatarBrowser showImage:_imageView];//调用方法

}


最后,一切OK,好了,结束。。。




0 0
原创粉丝点击