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

先來看看實現效果(GIF):

成都創新互聯公司專注為客戶提供全方位的互聯網綜合服務,包含不限于做網站、網站制作、札達網絡推廣、微信小程序定制開發、札達網絡營銷、札達企業策劃、札達品牌公關、搜索引擎seo、人物專訪、企業宣傳片、企業代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;成都創新互聯公司為所有大學生創業者提供札達建站搭建服務,24小時服務熱線:028-86922220,官方網址:www.kartarina.com

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

實現思路:

直接自定義 UIView(CYPhotoPreviewer),為了實現雙擊縮放,可以實現 UIScrollViewDelegate 對應的方法。如果需要模糊背景,可以在自定義的 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];
 
 // 設置模糊背景
 self.blurBackground = [[UIVisualEffectViewalloc] initWithEffect:[UIBlurEffecteffectWithStyle:UIBlurEffectStyleExtraLight]];
 self.blurBackground.frame = self.frame;
 [self addSubview:self.blurBackground];
 
 // 設置 UIScrollView 相關屬性
 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];
}

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

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

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

這兩種方式的實現都差不多,不過如果使用第一種方式的話,會導致將 CYPhotoPreviewer 添加到 keyWindow 上之后,再長按繼續將 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 傳遞進來,是為了顯示更好的動畫效果;將控制器的 container(self.view)傳遞進來,是為了將 CYPhotoPreviewer 添加到 container 的細節不需要在調用處處理,即初始化 CYPhotoPreviewer 之后,CYPhotoPreviewer 就直接被 container 添加為 subview 了。動畫很簡單不再細說。

顯示的效果已經做好,單擊關閉 CYPhotoPreviewer 也比較好實現,只需要從父類移除 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];
 }];
 }];
}

好了,顯示和關閉 CYPhotoPreviewer 都實現了,如果需要雙擊縮放圖片效果,就得實現 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 ,最好加入狀態判斷
 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: 這個方法會調用很頻繁,因此,為了避免 Attempt to present xxx on xxx which is already presenting xxx 這個警告,我們需要判斷手勢的狀態。

源碼下載

后話:

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

好了,以上就是這篇文章的全部內容了,希望本文的內容對各位iOS開發者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對創新互聯的支持。

名稱欄目:iOS實現點擊微信頭像(放大、縮放、保存)效果
標題URL:http://www.kartarina.com/article32/igcdpc.html

成都網站建設公司_創新互聯,為您提供域名注冊響應式網站網站營銷商城網站網站收錄關鍵詞優化

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站制作
主站蜘蛛池模板: 国模无码视频一区| 久久国产亚洲精品无码| (无码视频)在线观看| 无码少妇丰满熟妇一区二区 | 在线精品自偷自拍无码中文| 中文无码字幕中文有码字幕| 无码国模国产在线无码精品国产自在久国产 | 色欲香天天综合网无码| 精品无码国产自产在线观看水浒传| 无码人妻一区二区三区精品视频| 日韩午夜福利无码专区a| 免费无码又爽又刺激网站直播| 亚洲av专区无码观看精品天堂| 13小箩利洗澡无码视频网站免费 | 日韩免费无码一区二区视频 | 无码AV片在线观看免费| 本道天堂成在人线av无码免费| 亚洲中文字幕久久无码| 人妻无码久久一区二区三区免费| 成人免费无码H在线观看不卡| 永久免费无码网站在线观看| 亚洲熟妇无码av另类vr影视| 无码国产精品一区二区免费16| 一本加勒比HEZYO无码人妻 | 国产精品99久久久精品无码 | 91无码人妻精品一区二区三区L| 在线观看免费无码专区| 免费无码作爱视频| 亚洲精品无码不卡在线播放HE| 亚洲一区二区三区无码影院| 欧日韩国产无码专区| 无码少妇一区二区浪潮av| 无码午夜人妻一区二区不卡视频| 无码人妻丰满熟妇啪啪网站牛牛| 一本色道久久综合无码人妻| 亚洲AV无码专区国产乱码不卡 | 无码av大香线蕉伊人久久| 日韩人妻系列无码专区| 午夜无码视频一区二区三区| 日韩a级无码免费视频| 性无码专区无码片|