【iOS】實現一個帶動畫的簡單圖片檢視controller【更新】
1.單擊退出
2.雙擊放大/縮小
3.手勢縮放
4.按imageView原來frame動畫開啟/退出
1.原始碼
ImageBrowserController.h
#import <UIKit/UIKit.h>
typedef void (^ImageBrowserCompletion)();
@interface ImageBrowserController : UIViewController
-(instancetype)initWithPlaceImageView: (UIImageView*)imageView;
// 佔點陣圖
-(instancetype)initWithPlaceImageView: (UIImageView*)imageView
showCompletion: (ImageBrowserCompletion)show
dismissCompletion: (ImageBrowserCompletion)dismiss;
// 顯示
-(void)showWithAnimation;
@end
ImageBrowserController.m
#import "ImageBrowserController.h"
@interface ImageBrowserController ()<UIScrollViewDelegate>
@property(nonatomic,copy)UIScrollView *scrollView;
@property(nonatomic,copy)UIImageView *imageView;
@property(nonatomic)CGRect fromFrame;
@property(nonatomic)CGRect toFrame;
@end
@implementation ImageBrowserController
{
UIImageView *_placeImageView;
UIImage *_image;
ImageBrowserCompletion _showCompletion;
ImageBrowserCompletion _dismissCompletion;
}
-(instancetype)initWithPlaceImageView:(UIImageView *)imageView
showCompletion:(ImageBrowserCompletion)show
dismissCompletion:(ImageBrowserCompletion)dismiss{
self = [[ImageBrowserController alloc] initWithPlaceImageView:imageView];
_showCompletion = show;
_dismissCompletion = dismiss;
return self;
}
-(instancetype)initWithPlaceImageView:(UIImageView *)imageView{
if (self = [super init]) {
_placeImageView = imageView;
_image = imageView.image;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.scrollView];
}
-(void)showWithAnimation{
_placeImageView.image = nil;
_imageView.hidden = false;
_imageView.frame = self.fromFrame;
[UIView animateWithDuration:0.5 animations:^{
_imageView.frame = self.toFrame;
self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
} completion:^(BOOL finished) {
_showCompletion ? _showCompletion() : nil;
}];
}
-(void)closeWithAnimation{
_scrollView.contentOffset = CGPointZero;
_imageView.center = self.view.center;
[UIView animateWithDuration:0.5 animations:^{
_imageView.frame = self.fromFrame;
self.view.backgroundColor = [UIColor clearColor];
} completion:^(BOOL finished) {
_placeImageView.image = _image;
[self dismissViewControllerAnimated:false completion:^{
_dismissCompletion ? _dismissCompletion() : nil;
}];
}];
}
#pragma mark - getters & setters
-(UIScrollView *)scrollView{
if (!_scrollView) {
UIScrollView *sc = [[UIScrollView alloc] initWithFrame:self.view.bounds];
sc.minimumZoomScale = 0.5;
sc.maximumZoomScale = 3.0;
sc.delegate = self;
sc.showsVerticalScrollIndicator = false;
sc.showsHorizontalScrollIndicator = false;
[sc addSubview:self.imageView];
_scrollView = sc;
}
return _scrollView;
}
-(UIImageView *)imageView{
if (!_imageView) {
UIImageView *iv = [[UIImageView alloc] initWithImage:_placeImageView.image];
iv.hidden = true;
iv.userInteractionEnabled = true;
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
tap.numberOfTapsRequired=1;//單擊
tap.numberOfTouchesRequired=1;//單點觸碰
[iv addGestureRecognizer:tap];
UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired=2;
[tap requireGestureRecognizerToFail:doubleTap];//避免單擊與雙擊衝突
[iv addGestureRecognizer:doubleTap];
_imageView = iv;
}
return _imageView;
}
-(CGRect)fromFrame{
if (_fromFrame.size.width == 0) {
// 座標轉換
CGPoint org = [self.view convertPoint:_placeImageView.frame.origin fromView:_placeImageView.superview];
_fromFrame = CGRectMake(org.x, org.y, _placeImageView.frame.size.width, _placeImageView.frame.size.height);
}
return _fromFrame;
}
-(CGRect)toFrame{
if (_toFrame.size.width == 0)
{
CGFloat ratio = _image.size.height / _image.size.width;
CGFloat h = ratio * [self screenW];
_toFrame = CGRectMake(0, 0, [self screenW], h);
// 居中顯示
if (h > [self screenH]) {
_scrollView.contentSize = _imageView.bounds.size;
}
else{
CGFloat y = ([self screenH] - h) * 0.5;
_scrollView.contentInset = UIEdgeInsetsMake(y, 0, 0, y);
}
}
return _toFrame;
}
-(CGFloat)screenW{
return CGRectGetWidth([UIScreen mainScreen].bounds);
}
-(CGFloat)screenH{
return CGRectGetHeight([UIScreen mainScreen].bounds); // 狀態列
}
#pragma mark - UIScrollViewDelegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
// 注意: scrollview縮放內部的實現原理其實是利用transform實現的,
// 如果是利用transform縮放控制元件, 那麼bounds不會改變, 只有frame會改變
// 重新調整圖片的位置
CGFloat offsetY = ([self screenH] - _imageView.frame.size.height) * 0.5;
// 注意: 如果offsetY是負數, 會導致高度顯示不完整
offsetY = offsetY < 0 ? 0 : offsetY;
CGFloat offsetX = ([self screenW] - _imageView.frame.size.width) * 0.5;
// 注意: 如果offsetX負數, 會導致高度顯示不完整
offsetX = offsetX < 0 ? 0 : offsetX;
scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, offsetY, offsetX);
}
-(void)tap: (UITapGestureRecognizer*)gesture{
[self closeWithAnimation];
}
-(void)doubleTap: (UITapGestureRecognizer*)tap{
if (_scrollView.zoomScale < _scrollView.maximumZoomScale) {
[UIView animateWithDuration:0.5 animations:^{
_scrollView.zoomScale = _scrollView.maximumZoomScale;
}];
}
else{
[UIView animateWithDuration:0.5 animations:^{
_scrollView.zoomScale = 1.0;
}];
}
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)prefersStatusBarHidden{
return false;
}
@end
相關文章
- iOS 簡單實現帶圖片的LabeliOS
- php 實現一個簡單的圖片邊緣檢測PHP
- 簡單圖片檢視器小程式原始碼實現原始碼
- MXAlertView,一行程式碼實現iOS帶動畫的彈出檢視View行程iOS動畫
- 非常簡單的使用jqueryfancybox外掛實現的檢視圖片效果jQuery
- iOS_三種簡單動畫的實現iOS動畫
- 一個簡單的圖片水印工具
- 實現一個簡單的基於 WebAssembly 的圖片處理應用Web
- 實現簡單的輪播圖(單張圖片、多張圖片)
- 發現一個實現簡單地圖示註的視覺化軟體地圖視覺化
- 如何實現 iOS 16 帶來的 Depth Effect 圖片效果iOS
- [手寫系列] 帶你實現一個簡單的PromisePromise
- 一個封裝簡單使用簡單的雷達檢視封裝
- 系統學習iOS動畫之一:檢視動畫iOS動畫
- iOS專案開發實戰——實現檢視切換動畫iOS動畫
- 自己實現一個Controller——精簡型Controller
- WPF簡單動畫實現動畫
- 使用 PyQt5 實現圖片檢視器QT
- 如何實現一個簡易的圖片上傳Web ServerWebServer
- Dreamweaver之簡單實現網站佈局、圖片漂浮、區域跳轉、登陸註冊及圖片檢視器網站
- iOS (仿印物App)TableView給力動畫的簡單實現iOSAPPView動畫
- 實現一個帶下拉彈簧動畫的 ScrollView動畫View
- 一個簡單的載入動畫(一)動畫
- 如何實現一個簡單的雨滴動畫?手把手告訴你動畫
- 使用canvas實現簡單動畫Canvas動畫
- iOS感測器:實現一個隨螢幕旋轉的圖片iOS
- 讓動畫實現更簡單,Flutter 動畫簡易教程!動畫Flutter
- Android 網路圖片檢視顯示的實現方法Android
- jQuery實現的動畫簡單例項jQuery動畫單例
- 一個簡單的時間視窗設計與實現
- 實現一個簡單的視訊聊天室(原始碼)原始碼
- jquery實現的圖片預載入簡單介紹jQuery
- 原生JS實現最簡單的圖片懶載入JS
- 原生 JS 實現最簡單的圖片懶載入JS
- JQuery實現簡單美觀的圖片切換效果jQuery
- 一個簡單的載入動畫(二)動畫
- canvas簡單封裝一個echarts實現不了的餅圖Canvas封裝Echarts
- php實現一個簡單的socketPHP