【iOS】實現一個帶動畫的簡單圖片檢視controller【更新】

weixin_34208283發表於2016-12-09
1334681-36cc4e167a3ffc40.gif
只支援單張圖片手勢檢視

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

相關文章