九宫格图片浏览
思路:
1.放大
计算得到当前点击的imageView在windows的frame,
创建一个新的imageView在windows上并设置frame,根据image的宽高比计算出全屏显示时的高度和y值
动画放大
2.添加轮播图
移除windos上的imageView
创建添加scrollView在windows上,设置contentOffset.x(第几个imageView*屏幕宽度),根据传入的数组个数创建imageView,如果是长图的话要先创建一个scrollView再在上面创建一个imageView。
3.缩小
创建一个新的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创建一个新的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];
//创建添加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.创建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);
//创建一个新的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];
//回调
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