【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
相關文章
- php 實現一個簡單的圖片邊緣檢測PHP
- MXAlertView,一行程式碼實現iOS帶動畫的彈出檢視View行程iOS動畫
- 系統學習iOS動畫之一:檢視動畫iOS動畫
- 用canvas實現一個簡單的畫板Canvas
- 實現簡單的輪播圖(單張圖片、多張圖片)
- 自己實現一個Controller——精簡型Controller
- 實現一個簡單的基於 WebAssembly 的圖片處理應用Web
- 動手實現一個簡單的promisePromise
- 如何實現 iOS 16 帶來的 Depth Effect 圖片效果iOS
- 使用canvas實現簡單動畫Canvas動畫
- 讓動畫實現更簡單,Flutter 動畫簡易教程!動畫Flutter
- 發現一個實現簡單地圖示註的視覺化軟體地圖視覺化
- 如何實現一個簡單的雨滴動畫?手把手告訴你動畫
- 分享一個簡單的畫刷動畫效果:?️BrushEffect動畫
- [手寫系列] 帶你實現一個簡單的PromisePromise
- 自己動手實現一個簡單的 IOC
- Dreamweaver之簡單實現網站佈局、圖片漂浮、區域跳轉、登陸註冊及圖片檢視器網站
- 使用 PyQt5 實現圖片檢視器QT
- 如何實現一個簡易的圖片上傳Web ServerWebServer
- IOS橫線滾動檢視的實現---方式二iOS
- 前端動畫效果實現的簡單比較前端動畫
- 別用圖片了,CSS遮罩合成實現帶圓角的環形loading動畫CSS遮罩動畫
- 封裝一個簡單的動畫函式封裝動畫函式
- iOS實現字串動畫iOS字串動畫
- 實現一個簡單的TomcatTomcat
- 短視訊直播系統,簡單實現個人主頁背景圖片和文字顏色的更改
- 一個簡單的時間視窗設計與實現
- CSS 並不簡單–例項帶你領略實現SVG動畫的姿勢CSSSVG動畫
- 一個簡單的構建React元件動畫方案React元件動畫
- canvas簡單封裝一個echarts實現不了的餅圖Canvas封裝Echarts
- 僅用一個HTML標籤,實現帶動畫的抖音LOGOHTML動畫Go
- Flutter:手把手教你實現一個仿 Flipboard 圖片3D翻轉動畫Flutter3D動畫
- 使用jpeg圖片庫,顯示圖片並簡單實現LCD的觸屏功能
- 簡單的實現一個原型鏈原型
- 實現一個簡單的MVVM(Compile)MVVMCompile
- php實現一個簡單的socketPHP
- 實現一個簡單的 RESTful APIRESTAPI
- 如何自己動手實現一個圖片解答小助手
- 原生JS實現類似《掘金》網站的圖片檢視器JS網站