公司產(chǎn)品需要實(shí)現(xiàn)點(diǎn)擊個(gè)人主頁頭像可以放大頭像贵少、縮放頭像呵俏、保存頭像效果(和點(diǎn)擊微信個(gè)人頭像類似),故找個(gè)時(shí)間實(shí)現(xiàn)一下春瞬,記錄下來柴信,供自己查看,也給大家做個(gè)參考宽气。
實(shí)現(xiàn)效果(GIF):
實(shí)現(xiàn)思路:
直接自定義 UIView(CYPhotoPreviewer)随常,為了實(shí)現(xiàn)雙擊縮放,可以實(shí)現(xiàn) UIScrollViewDelegate 對(duì)應(yīng)的方法萄涯。如果需要模糊背景绪氛,可以在自定義的 UIView 中先添加模糊背景,再添加 UIScrollView涝影,繼而在 UIScrollView 中添加圖片容器枣察,這個(gè)容器就是要顯示的圖片的 superView,代碼一目了然:
- (void)setup {
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
[self addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
[singleTap requireGestureRecognizerToFail:doubleTap];
[self addGestureRecognizer:doubleTap];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self addGestureRecognizer:longPress];
// 設(shè)置模糊背景
self.blurBackground = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
self.blurBackground.frame = self.frame;
[self addSubview:self.blurBackground];
// 設(shè)置 UIScrollView 相關(guān)屬性
self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.scrollView.delegate = self;
self.scrollView.bouncesZoom = YES;
self.scrollView.maximumZoomScale = 3.0;
self.scrollView.multipleTouchEnabled = YES;
self.scrollView.alwaysBounceVertical = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:self.scrollView];
// containerView
self.containerView = [[UIView alloc] init];
[self.scrollView addSubview:self.containerView];
// imageView
self.imageView = [[UIImageView alloc] init];
self.imageView.clipsToBounds = YES;
self.imageView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
[self.containerView addSubview:self.imageView];
}
可以看到燃逻,我們給設(shè)置了模糊背景序目,給這個(gè) CYPhotoPreviewer 添加了單擊手勢(shì)(關(guān)閉 PhotoPreviewer)、雙擊手勢(shì)(縮放圖片)伯襟、長(zhǎng)按手勢(shì)(使用 UIAlertController 菜單猿涨,比如保存圖片等)。
好姆怪,確定了這個(gè) CYPhotoPreviewer 中的顯示內(nèi)容叛赚,那么我們?cè)撊绾物@示這個(gè) CYPhotoPreviewer 呢澡绩?
- 直接將這個(gè) CYPhotoPreviewer 添加到 keyWindow 上
- 將這個(gè) CYPhotoPreviewer 添加到控制器的 self.view 上
這兩種方式的實(shí)現(xiàn)都差不多娘香,不過如果使用第一種方式的話斜姥,會(huì)導(dǎo)致將 CYPhotoPreviewer 添加到 keyWindow 上之后梆砸,再長(zhǎng)按繼續(xù)將 UIAlertController 顯示就比較麻煩了氓轰,因此,這里打算采用將 CYPhotoPreviewer 添加到控制器的 self.view 上啡氢,繼而就可以很方便的顯示 UIAlertController 了:
- (void)previewFromImageView:(UIImageView *)fromImageView inContainer:(UIView *)container {
_fromImageView = fromImageView;
fromImageView.hidden = YES;
[container addSubview:self]; // 將 CYPhotoPreviewer 添加到 container 上
self.containerView.origin = CGPointZero;
self.containerView.width = self.width; // containerView 的寬度是屏幕的寬度
UIImage *image = fromImageView.image;
// 計(jì)算 containerView 的高度
if (image.size.height / image.size.height > self.height / self.width) {
self.containerView.height = floor(image.size.height / (image.size.width / self.width));
} else {
CGFloat height = image.size.height / image.size.width * self.width;
if (height < 1 || isnan(height)) height = self.height;
height = floor(height);
self.containerView.height = height;
self.containerView.centerY = self.height / 2;
}
if (self.containerView.height > self.height && self.containerView.height - self.height <= 1) {
self.containerView.height = self.height;
}
self.scrollView.contentSize = CGSizeMake(self.width, MAX(self.containerView.height, self.height));
[self.scrollView scrollRectToVisible:self.bounds animated:NO];
if (self.containerView.height <= self.height) {
self.scrollView.alwaysBounceVertical = NO;
} else {
self.scrollView.alwaysBounceVertical = YES;
}
CGRect fromRect = [fromImageView convertRect:fromImageView.bounds toView:self.containerView];
self.imageView.frame = fromRect;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.image = image;
[UIView animateWithDuration:0.18 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
self.imageView.frame = self.containerView.bounds;
self.imageView.transform = CGAffineTransformMakeScale(1.01, 1.01);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.18 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
self.imageView.transform = CGAffineTransformMakeScale(1.00, 1.00);
} completion:nil];
}];
}
可以看到孩等,我們將外面的圖片 fromImageView 傳遞進(jìn)來斥赋,是為了顯示更好的動(dòng)畫效果璃哟;將控制器的 container(self.view)傳遞進(jìn)來唠叛,是為了將 CYPhotoPreviewer 添加到 container 的細(xì)節(jié)不需要在調(diào)用處處理,即初始化 CYPhotoPreviewer 之后沮稚,CYPhotoPreviewer 就直接被 container 添加為 subview 了。動(dòng)畫很簡(jiǎn)單不再細(xì)說册舞。
顯示的效果已經(jīng)做好蕴掏,單擊關(guān)閉 CYPhotoPreviewer 也比較好實(shí)現(xiàn),只需要從父類移除 CYPhotoPreviewer 即可:
- (void)dismiss {
[UIView animateWithDuration:0.18 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect fromRect = [self.fromImageView convertRect:self.fromImageView.bounds toView:self.containerView];
self.imageView.contentMode = self.fromImageView.contentMode;
self.imageView.frame = fromRect;
self.blurBackground.alpha = 0.01;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.10 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.fromImageView.hidden = NO;
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}];
}
好了调鲸,顯示和關(guān)閉 CYPhotoPreviewer 都實(shí)現(xiàn)了盛杰,如果需要雙擊縮放圖片效果,就得實(shí)現(xiàn) UIScrollViewDelegate 的兩個(gè)方法以及 CYPhotoPreviewer 的雙擊手勢(shì):
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.containerView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
UIView *subView = self.containerView;
CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
(scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
(scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
subView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
scrollView.contentSize.height * 0.5 + offsetY);
}
- (void)doubleTap:(UITapGestureRecognizer *)recognizer {
if (self.scrollView.zoomScale > 1.0) {
[self.scrollView setZoomScale:1.0 animated:YES];
} else {
CGPoint touchPoint = [recognizer locationInView:self.imageView];
CGFloat newZoomScale = self.scrollView.maximumZoomScale;
CGFloat xSize = self.width / newZoomScale;
CGFloat ySize = self.height / newZoomScale;
[self.scrollView zoomToRect:CGRectMake(touchPoint.x - xSize / 2, touchPoint.y - ySize / 2, xSize, ySize) animated:YES];
}
}
最后一個(gè)就是長(zhǎng)按彈出菜單(UIAlertController)了:
- (void)longPress:(UILongPressGestureRecognizer *)recognizer {
// 為了避免彈警告:Warning: Attempt to present <UIAlertController: 0x7fcb1e619e80> on <ViewController: 0x7fcb1e60f9f0> which is already presenting <UIAlertController: 0x7fcb1e50d2e0>藐石,最好加入狀態(tài)判斷
if (recognizer.state == UIGestureRecognizerStateBegan) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"QuoraDots" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
UIViewController *vc = self.viewController;
[vc presentViewController:alertController animated:YES completion:nil];
}
}
注意一點(diǎn)即供,longPress:
這個(gè)方法會(huì)調(diào)用很頻繁,因此于微,為了避免 Attempt to present xxx on xxx which is already presenting xxx
這個(gè)警告逗嫡,我們需要判斷手勢(shì)的狀態(tài)。
后話:
這個(gè)只是顯示單張圖片的大圖株依,如果需要顯示多張圖片類似微信微博的九宮格圖片的大圖顯示驱证,則需要將這個(gè) CYPhotoPreviewer 搞成 UICollectionView 的 item 即可,大家可以嘗試嘗試恋腕。最后抹锄,如果需要或者 CYPhotoPreviewer 源碼,可以前往 GitHub 下載查看:https://github.com/angelen10/PhotoPreviewer