iOS 實(shí)現(xiàn)點(diǎn)擊微信頭像效果

公司產(chǎn)品需要實(shí)現(xiàn)點(diǎn)擊個(gè)人主頁(yè)頭像可以放大頭像、縮放頭像、保存頭像效果(和點(diǎn)擊微信個(gè)人頭像類似),故找個(gè)時(shí)間實(shí)現(xiàn)一下,記錄下來(lái),供自己查看,也給大家做個(gè)參考。

實(shí)現(xiàn)效果(GIF):

2017-03-08 09_42_20.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)來(lái),是為了顯示更好的動(dòng)畫效果;將控制器的 container(self.view)傳遞進(jìn)來(lái),是為了將 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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容