九宮格圖片瀏覽
思路:
1.放大
計算得到當前點擊的imageView在windows的frame,
創(chuàng)建一個新的imageView在windows上并設置frame放椰,根據(jù)image的寬高比計算出全屏顯示時的高度和y值
動畫放大
2.添加輪播圖
移除windos上的imageView
創(chuàng)建添加scrollView在windows上刑然,設置contentOffset.x(第幾個imageView*屏幕寬度)寺擂,根據(jù)傳入的數(shù)組個數(shù)創(chuàng)建imageView,如果是長圖的話要先創(chuàng)建一個scrollView再在上面創(chuàng)建一個imageView。
3.縮小
創(chuàng)建一個新的imageView在windows上計算frame
移除scrollView
動畫縮小后移除imageView
使用:
PhotoBrowser.h文件
#importtypedef void(^photoBrowserBlock)();
@interface PhotoBrowser : UIView
- (void)didSelectImageItem:(NSInteger)item andArray:(NSArray *)array;
@property (nonatomic ,copy) photoBrowserBlock block;
@end
PhotoBrowser.m文件
#import "PhotoBrowser.h"@interface? PhotoBrowser ()@end
@implementation PhotoBrowser
{
//當前頁碼
UILabel *pageLabel;
}
#define Alpha 0.85
- (void)didSelectImageItem:(NSInteger)item andArray:(NSArray *)array {
//遮蓋
UIView *cover = [[UIView alloc]initWithFrame:self.bounds];
cover.backgroundColor = COLOR_Black;
cover.alpha = 0;
[self addSubview:cover];
//1.在windows創(chuàng)建一個新的imageView,設置frame
UIImageView *imageView = [[UIImageView alloc]initWithFrame:[self returnCGRect:array[item]]];
imageView.image = [(UIImageView*)array[item] image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.layer.masksToBounds = YES;
[self addSubview:imageView];
//計算image的size
CGSize size = [self returnImageSize:imageView.image];
//計算imageView全屏顯示時的高度
NSInteger h = WIDTH*size.height/size.width;
[UIView animateWithDuration:0.5 animations:^{
if (size.width/size.height >= WIDTH/HEIGHT) {
//寬圖時y值
NSInteger y = (HEIGHT/2 - h/2);
//動畫放大寬圖
imageView.frame = CGRectMake(0, y, WIDTH, h);
cover.alpha = Alpha;
}
else {
//動畫放大長圖
imageView.frame = CGRectMake(0, 0, WIDTH, h);
cover.alpha = Alpha;
}
}
completion:^(BOOL finished) {
//移除windos上的imageView
[imageView removeFromSuperview];
//創(chuàng)建添加scrollView
[self setScrollItem:item andArray:array ];
}];
//頁碼顯示
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2, HEIGHT-50, WIDTH, 20)];
label.text = [NSString stringWithFormat:@"/%ld",array.count];
label.textColor = COLOR_While;
label.textAlignment = NSTextAlignmentLeft;
[self addSubview:label];
pageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, HEIGHT-50, WIDTH/2, 20)];
pageLabel.text = [NSString stringWithFormat:@"%ld",item+1];
pageLabel.textColor = COLOR_While;
pageLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:pageLabel];
}
- (void)setScrollItem:(NSInteger)item andArray:(NSArray *)array {
//2.創(chuàng)建scrollView
UIImageView *imv=[[UIImageView alloc]init];
imv.tag = i+1;
imv.contentMode = UIViewContentModeScaleAspectFit;
imv.image=[(UIImageView*)array[i] image];
imv.userInteractionEnabled=YES;
//計算image的size
CGSize size = [self returnImageSize:[(UIImageView*)array[i] image]];
if (size.width/size.height < WIDTH/HEIGHT) {
//展示長圖
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(x, 0, WIDTH, HEIGHT)];
[scroll addSubview:scrollView];
//計算高度
NSInteger h = WIDTH*size.height/size.width;
imv.frame =CGRectMake(0, 0, WIDTH, h);
scrollView.contentSize = imv.frame.size;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
[scrollView addSubview:imv];
}
else {
imv.frame = CGRectMake(x, 0, WIDTH, HEIGHT);
[scroll addSubview:imv];
}
//3.縮小
[imv whenTapped:^{
UIView *cover = [[UIView alloc]initWithFrame:self.bounds];
cover.backgroundColor = COLOR_Black;
cover.alpha = Alpha;
[WINDOWS_View addSubview:cover];
//計算image全屏顯示時的高度
NSInteger h = WIDTH*size.height/size.width;
//y值
NSInteger y = (HEIGHT/2 - h/2);
//創(chuàng)建一個新的imageView在windows上計算frame
UIImageView *imageview = [[UIImageView alloc]init];
if (size.width/size.height < WIDTH/HEIGHT)
imageview.frame = CGRectMake(0, 0, WIDTH, h);
else
imageview.frame = CGRectMake(0, y, WIDTH, h);
imageview.image = imv.image;
imageview.contentMode = UIViewContentModeScaleAspectFill;
imageview.layer.masksToBounds = YES;
[WINDOWS_View addSubview:imageview];
//移除scrollView
[scroll removeFromSuperview];
//回調(diào)
self.block();
//動畫縮小后移除imageView
[UIView animateWithDuration:0.5 animations:^{
imageview.frame = [self returnCGRect:array[imv.tag-1]];
cover.alpha = 0;
} completion:^(BOOL finished) {
[imageview removeFromSuperview];
[cover removeFromSuperview];
}];
}];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
pageLabel.text = [NSString stringWithFormat:@"%d",page+1];
}
//返回imageView在windows的frame
- (CGRect )returnCGRect:(UIImageView *)imageView {
return [imageView convertRect: imageView.bounds toView:WINDOWS];
}
//返回image的size
- (CGSize)returnImageSize:(UIImage*)image {
CGImageRef imageRef = [image CGImage];
return? CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
}
@end