欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實現(xiàn)點擊微信頭像(放大、縮放、保存)效果

 更新時間:2017年03月12日 11:17:52   投稿:daisy  
最近公司產(chǎn)品需要實現(xiàn)點擊個人主頁頭像可以放大頭像、縮放頭像、保存頭像效果(和點擊微信個人頭像類似),故找個時間實現(xiàn)一下,記錄下來,供自己查看也給有需要的大家做個參考。下面來一起看看吧。

先來看看實現(xiàn)效果(GIF):

實現(xiàn)思路:

直接自定義 UIView(CYPhotoPreviewer),為了實現(xiàn)雙擊縮放,可以實現(xiàn) UIScrollViewDelegate 對應(yīng)的方法。如果需要模糊背景,可以在自定義的 UIView 中先添加模糊背景,再添加 UIScrollView,繼而在 UIScrollView 中添加圖片容器,這個容器就是要顯示的圖片的 superView,代碼一目了然:

- (void)setup {
 
 self.frame = [UIScreenmainScreen].bounds;
 self.backgroundColor = [UIColorclearColor];
 
 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(singleTap:)];
 [self addGestureRecognizer:singleTap];
 
 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(doubleTap:)];
 doubleTap.numberOfTapsRequired = 2;
 [singleTaprequireGestureRecognizerToFail:doubleTap];
 [self addGestureRecognizer:doubleTap];
 
 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPress:)];
 [self addGestureRecognizer:longPress];
 
 // 設(shè)置模糊背景
 self.blurBackground = [[UIVisualEffectViewalloc] initWithEffect:[UIBlurEffecteffectWithStyle:UIBlurEffectStyleExtraLight]];
 self.blurBackground.frame = self.frame;
 [self addSubview:self.blurBackground];
 
 // 設(shè)置 UIScrollView 相關(guān)屬性
 self.scrollView = [[UIScrollViewalloc] initWithFrame:[UIScreenmainScreen].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 = [[UIViewalloc] init];
 [self.scrollViewaddSubview:self.containerView];
 
 // imageView
 self.imageView = [[UIImageViewalloc] init];
 self.imageView.clipsToBounds = YES;
 self.imageView.backgroundColor = [UIColorcolorWithWhite:1.0 alpha:0.5];
 [self.containerViewaddSubview:self.imageView];
}

可以看到,我們給設(shè)置了模糊背景,給這個 CYPhotoPreviewer 添加了單擊手勢(關(guān)閉 PhotoPreviewer)、雙擊手勢(縮放圖片)、長按手勢(使用 UIAlertController 菜單,比如保存圖片等)。

好,確定了這個 CYPhotoPreviewer 中的顯示內(nèi)容,那么我們該如何顯示這個 CYPhotoPreviewer 呢?

  1. 直接將這個 CYPhotoPreviewer 添加到 keyWindow 上
  2. 將這個 CYPhotoPreviewer 添加到控制器的 self.view 上

這兩種方式的實現(xiàn)都差不多,不過如果使用第一種方式的話,會導(dǎo)致將 CYPhotoPreviewer 添加到 keyWindow 上之后,再長按繼續(xù)將 UIAlertController 顯示就比較麻煩了,因此,這里打算采用將 CYPhotoPreviewer 添加到控制器的 self.view 上,繼而就可以很方便的顯示 UIAlertController 了:

- (void)previewFromImageView:(UIImageView *)fromImageViewinContainer:(UIView *)container {
 _fromImageView = fromImageView;
 fromImageView.hidden = YES;
 [containeraddSubview:self]; // 將 CYPhotoPreviewer 添加到 container 上
 
 self.containerView.origin = CGPointZero;
 self.containerView.width = self.width; // containerView 的寬度是屏幕的寬度
 
 UIImage *image = fromImageView.image;
 
 // 計算 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 {
 CGFloatheight = image.size.height / image.size.width * self.width;
 if (height self.height && self.containerView.height - self.height

可以看到,我們將外面的圖片 fromImageView 傳遞進(jìn)來,是為了顯示更好的動畫效果;將控制器的 container(self.view)傳遞進(jìn)來,是為了將 CYPhotoPreviewer 添加到 container 的細(xì)節(jié)不需要在調(diào)用處處理,即初始化 CYPhotoPreviewer 之后,CYPhotoPreviewer 就直接被 container 添加為 subview 了。動畫很簡單不再細(xì)說。

顯示的效果已經(jīng)做好,單擊關(guān)閉 CYPhotoPreviewer 也比較好實現(xiàn),只需要從父類移除 CYPhotoPreviewer 即可:

- (void)dismiss {
 [UIViewanimateWithDuration:0.18 delay:0.0 options:UIViewAnimationOptionCurveEaseInOutanimations:^{
 CGRectfromRect = [self.fromImageViewconvertRect:self.fromImageView.boundstoView:self.containerView];
 self.imageView.contentMode = self.fromImageView.contentMode;
 self.imageView.frame = fromRect;
 self.blurBackground.alpha = 0.01;
 } completion:^(BOOL finished) {
 [UIViewanimateWithDuration:0.10 delay:0 options:UIViewAnimationOptionCurveEaseInOutanimations:^{
 self.fromImageView.hidden = NO;
 self.alpha = 0;
 } completion:^(BOOL finished) {
 [self removeFromSuperview];
 }];
 }];
}

好了,顯示和關(guān)閉 CYPhotoPreviewer 都實現(xiàn)了,如果需要雙擊縮放圖片效果,就得實現(xiàn) UIScrollViewDelegate 的兩個方法以及 CYPhotoPreviewer 的雙擊手勢:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
 return self.containerView;
}
 
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
 UIView *subView = self.containerView;
 
 CGFloatoffsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
 (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
 
 CGFloatoffsetY = (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.scrollViewsetZoomScale:1.0 animated:YES];
 } else {
 CGPointtouchPoint = [recognizerlocationInView:self.imageView];
 CGFloatnewZoomScale = self.scrollView.maximumZoomScale;
 CGFloatxSize = self.width / newZoomScale;
 CGFloatySize = self.height / newZoomScale;
 [self.scrollViewzoomToRect:CGRectMake(touchPoint.x - xSize / 2, touchPoint.y - ySize / 2, xSize, ySize) animated:YES];
 }
}

最后一個就是長按彈出菜單(UIAlertController)了:

- (void)longPress:(UILongPressGestureRecognizer *)recognizer {
 
 // 為了避免彈警告:Warning: Attempt to present on which is already presenting ,最好加入狀態(tài)判斷
 if (recognizer.state == UIGestureRecognizerStateBegan) {
 UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"QuoraDots" message:nilpreferredStyle:UIAlertControllerStyleActionSheet];
 
 [alertControlleraddAction:[UIAlertActionactionWithTitle:@"保存" style:UIAlertActionStyleDefaulthandler:nil]];
 [alertControlleraddAction:[UIAlertActionactionWithTitle:@"取消" style:UIAlertActionStyleCancelhandler:nil]];
 
 UIViewController *vc = self.viewController;
 [vcpresentViewController:alertControlleranimated:YEScompletion:nil];
 }
}

注意一點, longPress: 這個方法會調(diào)用很頻繁,因此,為了避免 Attempt to present xxx on xxx which is already presenting xxx 這個警告,我們需要判斷手勢的狀態(tài)。

源碼下載

后話:

這個只是顯示單張圖片的大圖,如果需要顯示多張圖片類似微信微博的九宮格圖片的大圖顯示,則需要將這個 CYPhotoPreviewer 搞成 UICollectionView 的 item 即可,大家可以嘗試嘗試。

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位iOS開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • iOS開發(fā)微信收款到賬語音提醒功能思路詳解

    iOS開發(fā)微信收款到賬語音提醒功能思路詳解

    這篇文章主要介紹了iOS開發(fā)微信收款到賬語音提醒功能思路詳解,需要的朋友可以參考下
    2017-09-09
  • iOS tableView右側(cè)索引視圖狀態(tài)獲取的方法實例

    iOS tableView右側(cè)索引視圖狀態(tài)獲取的方法實例

    tableView用于顯示一個垂直滾動的單元格數(shù)(通常為可重復(fù)使用的單元格)組成的視圖,這篇文章主要給大家介紹了關(guān)于iOS tableView右側(cè)索引視圖狀態(tài)獲取的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • IOS 開發(fā)之實現(xiàn)取消tableView返回時cell選中的問題

    IOS 開發(fā)之實現(xiàn)取消tableView返回時cell選中的問題

    這篇文章主要介紹了IOS 開發(fā)之實現(xiàn)取消tableView返回時cell選中的問題的相關(guān)資料,希望通過本文能實現(xiàn)大家想要的功能,需要的朋友可以參考下
    2017-09-09
  • iOS橫屏彈鍵盤的高度錯誤異常解決

    iOS橫屏彈鍵盤的高度錯誤異常解決

    這篇文章主要給大家介紹了關(guān)于iOS橫屏彈鍵盤的高度錯誤異常解決的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • iOS時間字符串格式化輸出技巧詳解

    iOS時間字符串格式化輸出技巧詳解

    本篇文章主要介紹了iOS時間格式化輸出技巧,可以將后臺返回的時間字符串轉(zhuǎn)換為指定的格式時間再顯示在UI上,有興趣的可以了解一下。
    2017-04-04
  • 如何使用IOS實現(xiàn)WIFI傳輸

    如何使用IOS實現(xiàn)WIFI傳輸

    這篇文章主要介紹了如何使用IOS實現(xiàn)WIFI傳輸,對局域網(wǎng)傳輸和HTTP感興趣的同學(xué),可以參考下
    2021-04-04
  • iOS實現(xiàn)視頻下載并自動保存到相冊功能

    iOS實現(xiàn)視頻下載并自動保存到相冊功能

    這篇文章主要為大家詳細(xì)介紹了ios 視頻下載功能實現(xiàn),并自動保存到相冊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 詳解iOS的沖頂大會輔助

    詳解iOS的沖頂大會輔助

    本篇文章主要介紹了詳解iOS的沖頂大會輔助,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 詳解IOS中Tool Bar切換視圖方法

    詳解IOS中Tool Bar切換視圖方法

    這篇文章主要介紹了詳解IOS中Tool Bar切換視圖方法以及實例代碼分析,需要的朋友學(xué)習(xí)一下吧。
    2017-12-12
  • 提高iOS開發(fā)的小技巧和思路小結(jié) (二)

    提高iOS開發(fā)的小技巧和思路小結(jié) (二)

    這篇文章主要跟大家分享了關(guān)于提高iOS開發(fā)的一些小技巧和思路,通過本文總結(jié)的這些小技巧和思路相信對對大家開發(fā)iOS具有一定的參考價值,感興趣的朋友們可以參考學(xué)習(xí),下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04

最新評論