IOS中實(shí)現(xiàn)圖片點(diǎn)擊全屏預(yù)覽
如果你感覺(jué)累,那就對(duì)了那是因?yàn)槟阍谧呱掀侣贰?。這句話似乎有點(diǎn)道理的樣子,時(shí)常提醒自己無(wú)論走到哪都不要忘記自己當(dāng)初為什么出發(fā)。有時(shí)想想感覺(jué)有的東西可以記錄一下,就把它記錄下來(lái)吧,這次想寫(xiě)一下關(guān)于單張圖片點(diǎn)擊全屏預(yù)覽的問(wèn)題,網(wǎng)上查了一些大神寫(xiě)的有的功能確實(shí)很強(qiáng)大但自己暫時(shí)想要的只是簡(jiǎn)單的功能就好,還有些方法自己也沒(méi)弄出想要的效果,最后寫(xiě)了一個(gè)比較簡(jiǎn)單的點(diǎn)擊單張圖片的全屏預(yù)覽和雙指捏合縮小放大,可能有時(shí)要對(duì)圖片做一些處理,這里放大后只是顯示同一張圖片并未做處理,下面直接貼出代碼
// // ViewController.m // XWZoomImageView // // Created by xiao on 15/11/13. // Copyright © 2015年 xiao. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIImageView *picView; @property (weak, nonatomic) UIScrollView *scrollView; @property (weak, nonatomic) UIImageView *lastImageView; @property (nonatomic, assign)CGRect originalFrame; @property (nonatomic, assign)BOOL isDoubleTap; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.picView.userInteractionEnabled = YES; //添加單擊手勢(shì) UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)]; [self.picView addGestureRecognizer:tap]; } -(void)showZoomImageView:(UITapGestureRecognizer *)tap { if (![(UIImageView *)tap.view image]) { return; } //scrollView作為背景 UIScrollView *bgView = [[UIScrollView alloc] init]; bgView.frame = [UIScreen mainScreen].bounds; bgView.backgroundColor = [UIColor blackColor]; UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)]; [bgView addGestureRecognizer:tapBg]; UIImageView *picView = (UIImageView *)tap.view; UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = picView.image; imageView.frame = [bgView convertRect:picView.frame fromView:self.view]; [bgView addSubview:imageView]; [[[UIApplication sharedApplication] keyWindow] addSubview:bgView]; self.lastImageView = imageView; self.originalFrame = imageView.frame; self.scrollView = bgView; //最大放大比例 self.scrollView.maximumZoomScale = 1.5; self.scrollView.delegate = self; [UIView animateWithDuration:0.5 animations:^{ CGRect frame = imageView.frame; frame.size.width = bgView.frame.size.width; frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width); frame.origin.x = 0; frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5; imageView.frame = frame; }]; } -(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer { self.scrollView.contentOffset = CGPointZero; [UIView animateWithDuration:0.5 animations:^{ self.lastImageView.frame = self.originalFrame; tapBgRecognizer.view.backgroundColor = [UIColor clearColor]; } completion:^(BOOL finished) { [tapBgRecognizer.view removeFromSuperview]; self.scrollView = nil; self.lastImageView = nil; }]; } //返回可縮放的視圖 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.lastImageView; }
最后同樣帶上一張圖片吧,大致是這樣子
再給大家分享一則IOS中點(diǎn)擊圖片后放大的代碼
.h文件----------------------------------------------------------------- #import @interface ZoomImage : NSObject /** *@brief點(diǎn)擊圖片放大,再次點(diǎn)擊縮小 * *@param oldImageView 頭像所在的imageView */ +(void)showImage:(UIImageView*)avatarImageView; @end .m文件----------------------------------------------------------------- #import "ZoomImage.h" static CGRect oldframe; @implementation ZoomImage +(void)showImage:(UIImageView*)avatarImageView { UIImage *image =avatarImageView.image; // 獲得根窗口 UIWindow *window =[UIApplication sharedApplication].keyWindow; UIView *backgroundView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)]; oldframe =[avatarImageView convertRect:avatarImageView.bounds toView:window]; backgroundView.backgroundColor =[UIColor blackColor]; backgroundView.alpha =0.5; UIImageView *imageView =[[UIImageView alloc]initWithFrame:oldframe]; imageView.image =image; imageView.tag =1; [backgroundView addSubview:imageView]; [window addSubview:backgroundView]; //點(diǎn)擊圖片縮小的手勢(shì) UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)]; [backgroundView addGestureRecognizer:tap]; [UIView animateWithDuration:0.3 animations:^{ imageView.frame =CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width); backgroundView.alpha =1; }]; } +(void)hideImage:(UITapGestureRecognizer *)tap{ UIView *backgroundView =tap.view; UIImageView *imageView =(UIImageView *)[tap.view viewWithTag:1]; [UIView animateWithDuration:0.3 animations:^{ imageView.frame =oldframe; backgroundView.alpha =0; } completion:^(BOOL finished) { [backgroundView removeFromSuperview]; }]; }
相關(guān)文章
詳解iOS App中調(diào)用AVAudioPlayer播放音頻文件的用法
這篇文章主要介紹了iOS App中調(diào)用AVAudioPlayer播放音頻文件的用法,AVAudioPlayer僅能播放本地文件而不能添加網(wǎng)絡(luò)源,實(shí)例代碼為Objective-C,需要的朋友可以參考下2016-03-03iOS UIBezierPath實(shí)現(xiàn)餅狀圖
這篇文章主要為大家詳細(xì)介紹了iOS UIBezierPath實(shí)現(xiàn)餅狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03IOS Ble藍(lán)牙開(kāi)發(fā)實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了IOS Ble藍(lán)牙開(kāi)發(fā)的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12IOS 開(kāi)發(fā)狀態(tài)欄隱藏的實(shí)現(xiàn)辦法
這篇文章主要介紹了IOS 開(kāi)發(fā)狀態(tài)欄隱藏的實(shí)現(xiàn)辦法的相關(guān)資料,需要的朋友可以參考下2017-02-02詳解iOS應(yīng)用程序的啟動(dòng)過(guò)程
這篇文章主要介紹了iOS應(yīng)用程序的啟動(dòng)過(guò)程,講述了從其執(zhí)行main函數(shù)開(kāi)始到展示UIWindow的流程中的一些關(guān)鍵點(diǎn),需要的朋友可以參考下2016-03-03IOS中Weex 加載 .xcassets 中的圖片資源的實(shí)例詳解
這篇文章主要介紹了IOS中Weex 加載 .xcassets 中的圖片資源的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文介紹能幫助到大家,實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08iOS中設(shè)置父視圖透明但內(nèi)容不透明的方法
設(shè)置一定的背景透明會(huì)讓用戶的體驗(yàn)非常不錯(cuò),下面這篇文章就主要跟大家分享了iOS中設(shè)置父視圖透明但內(nèi)容不透明的方法,文中給出了詳細(xì)的示例代碼,需要的朋友們下面來(lái)一起看看吧。2017-05-05iOS如何獲取漢字(簡(jiǎn)體中文)筆畫(huà)數(shù)詳解
這篇文章主要給大家介紹了關(guān)于iOS如何獲取漢字(簡(jiǎn)體中文)筆畫(huà)數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02