国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS中實現圖片全屏預覽的功能

2020-02-19 15:48:53
字體:
來源:轉載
供稿:網友

ios是一款智能手機系統,我們在查看圖片的時候,全屏預覽這個功能為用戶們帶來了很好的視覺體驗,那么你知道如何實現全屏預覽這個小工具的嗎?現在我們去看看IOS中實現圖片全屏預覽的功能的內容。

//// 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; //添加單擊手勢 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中點擊圖片后放大的代碼

.h文件-----------------------------------------------------------------#import@interface ZoomImage : NSObject/** *@brief點擊圖片放大,再次點擊縮小 * *@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]; //點擊圖片縮小的手勢 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]; }];}

上面就是小編整理的IOS中實現圖片全屏預覽的功能的代碼給大家來分享一下,,需要的朋友可以參考下

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴安盟| 黑水县| 桦甸市| 浦县| 桐庐县| 西城区| 抚州市| 高要市| 华池县| 股票| 托克逊县| 繁峙县| 大埔县| 满城县| 天峻县| 宜良县| 滨州市| 临安市| 凭祥市| 拜泉县| 察隅县| 桂阳县| 舒兰市| 汤原县| 城口县| 广河县| 新野县| 沧源| 沭阳县| 凤山县| 福建省| 大悟县| 肇庆市| 揭西县| 岳阳市| 景德镇市| 准格尔旗| 江北区| 阜阳市| 蓝田县| 油尖旺区|