項目中顯示后臺下發(fā)的縮略圖指郁,點擊縮略圖放大全屏查看忙上,現(xiàn)在是直接放大圖片,當(dāng)然你也可以使用在分頁的UIScrollView上添加圖片來進行展示闲坎。
- 步驟分為三步
1.創(chuàng)建UIImageView
注:
采用EGOImageView
是因為加載的是url疫粥,直接引用的三方庫:
pod 'EGOImageLoading', '~> 0.0.1'
//初始化要顯示的圖片內(nèi)容的imageView(這里位置是使用當(dāng)前屏幕的 寬和高宏定義)
EGOImageView *imgView = [[EGOImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
//圖片背景色設(shè)置成黑色(必須)
imgView.backgroundColor = [UIColor blackColor];
//要顯示的圖片,即要放大的圖片(放大圖片的填充方式)
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImageURL:[NSURL URLWithString:imgArray[sender.tag]]];
imgView.userInteractionEnabled = YES;
2.以動畫方式添加到window上
UIWindow * window = [UIApplication sharedApplication].keyWindow;
[window addSubview:imgView];
[self shakeToShow:imgView];
//放大過程中出現(xiàn)的緩慢動畫
- (void) shakeToShow:(UIView*)aView{
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.2;
NSMutableArray *values = [NSMutableArray array];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
animation.values = values;
[aView.layer addAnimation:animation forKey:nil];
}
3.在UIImageView上添加手勢腰懂,點擊取消顯示當(dāng)前圖片
//添加點擊手勢(即點擊圖片后退出全屏)
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView:)];
[imgView addGestureRecognizer:tapGesture];
- (void)closeView:(UITapGestureRecognizer *)tap {
[tap.view removeFromSuperview];
}