你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS中完成圖片點擊全屏預覽

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中完成圖片點擊全屏預覽】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved