關於點選小圖片之後檢視對應大圖的實現
在此,封裝了一個類,外面用的話直接呼叫方法,即可實現想要的效果。但是有一點,點進去之後只能檢視對應的一張圖,不能滑動,如果想要更多的效果,自己在此基礎上進行再封裝吧。只做參考。程式碼如下:
.h檔案中:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface SJAvatarBrowser : NSObject
+(void)showImage:(UIImageView *)avatarImageView;
.m檔案中:
#import "SJAvatarBrowser.h"
staticCGRect oldframe;
@implementation SJAvatarBrowser
+(void)showImage:(UIImageView *)avatarImageView{
UIImage *image=avatarImageView.image;
UIWindow *window=[UIApplicationsharedApplication].keyWindow;
UIView *backgroundView=[[UIViewalloc]initWithFrame:CGRectMake(0,
0, [UIScreenmainScreen].
oldframe=[avatarImageView convertRect:avatarImageView.boundstoView:window];
backgroundView.backgroundColor=[UIColorblackColor];
backgroundView.alpha=0;
UIImageView *imageView=[[UIImageViewalloc]initWithFrame:oldframe];
imageView.
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, [UIScreenmainScreen].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"
@interfaceViewController () {
UIImageView *_imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// 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,好了,結束。。。