123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- //
- // SPSBPreviewViewController.m
- // 我的社保
- //
- // Created by shanp on 2021/5/18.
- //
- #import "SPSBPreviewViewController.h"
- #import "SPSBUIGeneralHeader.h"
- #import "JXHZoomImageView.h"
- #import "SPSBEnumerationProfile.h"
- #import <SDWebImage.h>
- #import "SPSBURLApiProfile.h"
- @implementation SPSBPreviewConfiguration
- @end
- @interface SPSBPreviewViewController ()<UIScrollViewDelegate, UIGestureRecognizerDelegate> {
- UIImageView *_animationView;
-
- UIView *_bgView;
- UIScrollView *_scrollView;
- UIPageControl *_pageControl;
-
- // BYHVideoPlayer *_player;
- // AVPlayerLayer *_playLayer;
- // UIView *_playView;
-
- NSInteger _index;
-
- SPSBPreviewConfiguration *_configuration;
-
- CGFloat _panOriginalWidth;
- CGFloat _panOriginalHeight;
- CGFloat _panOriginalX;
- CGFloat _panOriginalY;
- }
- @end
- #define spsb_offset 20
- #define spsb_imageViewTag 5000
- @implementation SPSBPreviewViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // [self initData];
- [self setupUI];
- }
- //- (void)initData {
- // _player = SPSBVideoPlayer.new;
- // _player.spsb_isLoop = _player.spsb_isAutoPlay = true;
- // _player.spsb_videoDelegate = self;
- //}
- #pragma mark - Action
- - (void)showWithConfiguration:(SPSBPreviewConfiguration *)configuration currentImage:(UIImage *)currentImage index:(NSInteger)index {
- [self.view.superview bringSubviewToFront:self.view];
- _index = index;
- _configuration = configuration;
- // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
- // _playView.hidden = false;
- // if (!_configuration.spsb_video) {
- // [self.view showToastWithTitle:SPSBLocalize(SPSBVideoPlayFailureTipsTextStringKey)];
- // return;
- // }
- // _pageControl.numberOfPages = 1;
- // _pageControl.currentPage = 0;
- // [_scrollView setContentSize:(CGSize){jxh_screenWidth() + spsb_offset, jxh_screenHeight()}];
- // [_scrollView setContentOffset: (CGPoint){0, 0}];
- // for (NSInteger i = 0; i < _spsb_maxPreview; i ++) {
- // JXHZoomImageView *imageView = [_scrollView viewWithTag:spsb_imageViewTag + i];
- // if (i == index) {
- // [imageView setImage:currentImage];
- // } else {
- // [imageView setImage:nil];
- // }
- // }
- // } else {
- // _playView.hidden = true;
- NSInteger count = _configuration.spsb_images.count > _spsb_maxPreview ? _spsb_maxPreview : _configuration.spsb_images.count;
- if (index >= count) index = count - 1;
- _pageControl.numberOfPages = count;
- _pageControl.currentPage = index;
- [_scrollView setContentSize:(CGSize){(jxh_screenWidth() + spsb_offset) * count, jxh_screenHeight()}];
- [_scrollView setContentOffset: (CGPoint){(jxh_screenWidth() + spsb_offset) * index, 0}];
- for (NSInteger i = 0; i < _spsb_maxPreview; i ++) {
- JXHZoomImageView *imageView = [_scrollView viewWithTag:spsb_imageViewTag + i];
- [imageView setImage:nil];
- if (i == index) {
- [imageView setImage:currentImage];
- } else if (i < _configuration.spsb_images.count) {
- @weakify(imageView)
- [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:spsb_fileURL(configuration.spsb_images[i]) completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
- if (image) {
- [weak_imageView setImage:image];
- }
- }];
- }
- }
- // }
-
-
- CGRect rect = [self getImageOriginalRect];
-
- _animationView.image = currentImage;
- [_animationView setLayerCornerRadius:configuration.spsb_cornerRadius clipToBounds:true];
- _animationView.frame = rect;
-
- CGRect newRect = [self getAnimationImageViewRect];
-
- self.view.backgroundColor = spsb_000000_color(1.f);
- self.view.hidden = _animationView.hidden = false;
- _spsb_previewing = true;
- [self setNeedsStatusBarAppearanceUpdate];
- [UIView animateWithDuration:0.3 animations:^{
- self->_animationView.frame = newRect;
- [self->_animationView setLayerCornerRadius:0 clipToBounds:true];
- } completion:^(BOOL finished) {
- self->_bgView.hidden = false;
- // if (configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
- // self->_playView.frame = self->_animationView.frame;
- // self->_playLayer = [self->_player setPlayUrl:configuration.spsb_video videoGravity:AVLayerVideoGravityResizeAspectFill layerFrame:self->_animationView.bounds];
- // [self->_playView.layer addSublayer:self->_playLayer];
- // } else {
- self->_animationView.hidden = true;
- // }
- }];
- }
- - (void)dismissView {
- JXHZoomImageView *imageView = [_scrollView viewWithTag:spsb_imageViewTag + _index];
- _animationView.image = imageView.jxh_imageView.image;
- CGRect rect = [self getAnimationImageViewRect];
- _animationView.frame = rect;
-
- CGRect newRect = [self getImageOriginalRect];
-
- // if (_playLayer) {
- // [_player playOperate:SPSBVideoOperateStop];
- // [_playLayer removeFromSuperlayer];
- // _playLayer = nil;
- // }
- _bgView.hidden = true;
- _animationView.hidden = false;
- _spsb_previewing = false;
-
- [UIView animateWithDuration:0.3 animations:^{
- self->_animationView.frame = newRect;
- [self->_animationView setLayerCornerRadius:self->_configuration.spsb_cornerRadius clipToBounds:true];
- [self setNeedsStatusBarAppearanceUpdate];
- self.view.backgroundColor = spsb_000000_color(0.f);
- } completion:^(BOOL finished) {
- self->_animationView.image = nil;
- self.view.hidden = self->_animationView.hidden = true;
- }];
- }
- - (void)dismissViewPan {
- // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
- // _animationView.frame = _playView.frame;
- // }
-
- CGRect newRect = [self getImageOriginalRect];
-
- // if (_playLayer) {
- // [_player playOperate:SPSBVideoOperateStop];
- // [_playLayer removeFromSuperlayer];
- // _playLayer = nil;
- // }
- _bgView.hidden = true;
- _animationView.hidden = false;
- _spsb_previewing = false;
-
- [UIView animateWithDuration:0.3 animations:^{
- self->_animationView.frame = newRect;
- [self->_animationView setLayerCornerRadius:self->_configuration.spsb_cornerRadius clipToBounds:true];
- [self setNeedsStatusBarAppearanceUpdate];
- self.view.backgroundColor = spsb_000000_color(0.f);
- } completion:^(BOOL finished) {
- self->_animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
- // self->_playView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
- self->_animationView.image = nil;
- self.view.hidden = self->_animationView.hidden = true;
- }];
- }
- - (CGRect)getAnimationImageViewRect {
- UIImage *image = _animationView.image;
- if (!image) {
- return (CGRect){0, 0, jxh_screenWidth(), jxh_screenHeight()};
- }
- // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
- // CGFloat proportion = jxh_viewHeight(self.view) / jxh_viewWidth(self.view) - image.size.height / image.size.width;
- // if (proportion > 0 && proportion < SPSBVideoFullScreenDifferentProportion) {
- // CGFloat width = image.size.width / image.size.height * jxh_screenHeight();
- // CGFloat x = (jxh_screenWidth() - width) / 2;
- // return (CGRect){x, 0, width, jxh_screenHeight()};
- // } else {
- // CGFloat height = image.size.height / image.size.width * jxh_screenWidth();
- // CGFloat y = (jxh_screenHeight() - height) / 2;
- // return (CGRect){0, y, jxh_screenWidth(), height};
- // }
- // } else {
- CGFloat height = image.size.height / image.size.width * jxh_screenWidth();
- CGFloat y = (jxh_screenHeight() - height) / 2;
- if (y < 0) y = 0;
- return (CGRect){0, y, jxh_screenWidth(), height};
- // }
- }
- - (CGRect)getAnimationImageViewPanRectWithView:(JXHZoomImageView *)view {
- UIImage *image = _animationView.image;
- if (!image) {
- return (CGRect){0, 0, jxh_screenWidth(), jxh_screenHeight()};
- }
- // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
- // CGFloat proportion = jxh_viewHeight(self.view) / jxh_viewWidth(self.view) - image.size.height / image.size.width;
- // if (proportion > 0 && proportion < SPSBVideoFullScreenDifferentProportion) {
- // CGFloat width = image.size.width / image.size.height * jxh_screenHeight();
- // CGFloat x = (jxh_screenWidth() - width) / 2;
- // return (CGRect){x, 0, width, jxh_screenHeight()};
- // } else {
- // CGFloat height = image.size.height / image.size.width * jxh_screenWidth();
- // CGFloat y = (jxh_screenHeight() - height) / 2;
- // return (CGRect){0, y, jxh_screenWidth(), height};
- // }
- // } else {
- CGFloat height = image.size.height / image.size.width * jxh_viewWidth(view.jxh_imageView);
- CGFloat y = (jxh_screenHeight() - height) / 2;
- if (y < 0) y = 0;
- return (CGRect){-view.contentOffset.x, y, jxh_viewWidth(view.jxh_imageView), height};
- // }
- }
- - (CGRect)getImageOriginalRect {
- if (_spsb_getOriginalRect) {
- return _spsb_getOriginalRect(_index);
- } else {
- return CGRectZero;
- }
- }
- - (void)panAction:(UIPanGestureRecognizer *)sender {
- // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo && !_player) {
- // sender.state = UIGestureRecognizerStateFailed;
- // return;
- // }
- CGPoint p = [sender translationInView:self.view];
- JXHZoomImageView *imageView = [_scrollView viewWithTag:spsb_imageViewTag + _index];
- if (!imageView.jxh_imageView.image) {
- sender.state = UIGestureRecognizerStateFailed;
- return;
- }
- CGFloat scale = 1 - p.y / jxh_viewHeight(self.view) / 2;
- if (scale > 1) {
- scale = 1.f;
- }
-
- switch (sender.state) {
- case UIGestureRecognizerStateBegan: {
- if ([sender translationInView:self.view].x != 0) {
- sender.state = UIGestureRecognizerStateFailed;
- return;
- }
- if (imageView.contentOffset.y + imageView.contentInset.top > 1) {
- sender.state = UIGestureRecognizerStateFailed;
- return;
- }
- if (p.y <= 0) {
- sender.state = UIGestureRecognizerStateFailed;
- return;
- }
- _scrollView.panGestureRecognizer.state = UIGestureRecognizerStateFailed;
- imageView.panGestureRecognizer.state = UIGestureRecognizerStateFailed;
- // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
- // _bgView.hidden = true;
- // _animationView.hidden = true;
- // [_player playOperate:SPSBVideoOperatePause];
- // _panOriginalWidth = jxh_viewWidth(_playView);
- // _panOriginalHeight = jxh_viewHeight(_playView);
- // CGPoint location = [sender locationInView:_playView];
- // _panOriginalX = location.x;
- // _panOriginalY = location.y;
- // _playView.transform = CGAffineTransformMake(scale, 0, 0, scale, p.x, p.y);
- // } else {
- _animationView.image = imageView.jxh_imageView.image;
- _animationView.frame = [self getAnimationImageViewPanRectWithView:imageView];
- _panOriginalWidth = jxh_viewWidth(_animationView);
- _panOriginalHeight = jxh_viewHeight(_animationView);
- CGPoint location = [sender locationInView:_animationView];
- _panOriginalX = location.x;
- _panOriginalY = location.y;
- debugLog(@"%lf----%lf", location.x, location.y);
- _animationView.hidden = false;
- _bgView.hidden = true;
- _animationView.transform = CGAffineTransformMake(scale, 0, 0, scale, p.x, p.y);
- // }
-
- self.view.backgroundColor = spsb_000000_color(scale);
-
- }
- break;
- case UIGestureRecognizerStateChanged: {
- CGFloat change = (1 - scale);
- CGFloat y = p.y - (_panOriginalHeight / 2 - _panOriginalY) * change;
- CGFloat x = p.x - (_panOriginalWidth / 2 - _panOriginalX) * change;
- // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
- // _playView.transform = CGAffineTransformMake(scale, 0, 0, scale, x, y);
- // } else {
- _animationView.transform = CGAffineTransformMake(scale, 0, 0, scale, x, y);
- // }
- self.view.backgroundColor = spsb_000000_color(scale);
- }
- break;
- case UIGestureRecognizerStateFailed:
- // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
- // _playView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
- // _bgView.hidden = false;
- // [_player playOperate:SPSBVideoOperatePlay];
- // } else {
- _animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
- _animationView.hidden = true;
- _bgView.hidden = false;
- // }
- self.view.backgroundColor = spsb_000000_color(1.f);
- break;
- default:
- if ([sender velocityInView:self.view].y > 1000) {
- [self dismissViewPan];
- return;
- }
- if (p.y > jxh_viewHeight(self.view) / 2) {
- [self dismissViewPan];
- return;
- }
- // if (_configuration.spsb_type == SPSBPersonalPagePreviewTypeVideo) {
- // [UIView animateWithDuration:0.3 animations:^{
- // self->_playView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
- // self.view.backgroundColor = spsb_000000_color(1.f);
- // } completion:^(BOOL finished) {
- // self->_bgView.hidden = false;
- // [self->_player playOperate:SPSBVideoOperatePlay];
- // }];
- // } else {
- [UIView animateWithDuration:0.3 animations:^{
- self->_animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
- self.view.backgroundColor = spsb_000000_color(1.f);
- } completion:^(BOOL finished) {
- self->_animationView.hidden = true;
- self->_bgView.hidden = false;
- }];
- // }
- break;
- }
- }
- //#pragma mark - SPSBNotificationDelegate
- //- (void)spsb_shouldEnterRoom {
- // [self dismissView];
- //}
- #pragma mark - UIScrollViewDelegate
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
- _pageControl.currentPage = scrollView.contentOffset.x / (jxh_screenWidth() + spsb_offset);
- }
- - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
- if (scrollView != _scrollView) return;
- if (!decelerate) {
- [self endScroll:scrollView];
- }
-
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- if (scrollView != _scrollView) return;
- [self endScroll:scrollView];
- }
- - (void)endScroll:(UIScrollView *)scrollView {
- _index = scrollView.contentOffset.x / (jxh_screenWidth() + spsb_offset);
- _pageControl.currentPage = _index;
- }
- //#pragma mark - SPSBVideoPlayerDelegate
- //- (void)spsb_videoPlayFailed:(NSError *)error {
- // [self.view showToastWithTitle:SPSBLocalize(SPSBVideoPlayFailureTipsTextStringKey)];
- // [self dismissView];
- //}
- //
- //- (void)spsb_videoPlayStatusChanged:(SPSBVideoStatus)status {
- //
- //}
- #pragma mark - UIGestureRecognizerDelegate
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
- if ([otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
- return false;
- }
- return true;
- }
- #pragma mark - UI
- - (void)setupUI {
- self.view.backgroundColor = spsb_000000_color(0.f);
- self.view.hidden = true;
-
- [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)]];
-
- UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
- panGes.delegate = self;
- [self.view addGestureRecognizer:panGes];
- [self createBgView];
-
- _animationView = UIImageView.new;
- _animationView.clipsToBounds = true;
- _animationView.contentMode = UIViewContentModeScaleAspectFill;
- [self.view addSubview:_animationView];
-
- // _playView = UIView.new;
- // [self.view addSubview:_playView];
- }
- - (void)createBgView {
- _bgView = UIView.new;
- _bgView.hidden = true;
- [self.view addSubview:_bgView];
- [_bgView makeConstraints:^(JXHConstraintMaker *make) {
- make.edges.equalTo(self.view);
- }];
-
- _scrollView = [UIScrollView convenienceWithBackgroundColor:spsb_clear_color()];
- _scrollView.pagingEnabled = true;
- _scrollView.delegate = self;
- [_bgView addSubview:_scrollView];
- [_scrollView makeConstraints:^(JXHConstraintMaker *make) {
- make.edges.equalTo((UIEdgeInsets){0, 0, 0, -spsb_offset});
- }];
-
- for (NSInteger i = 0; i < _spsb_maxPreview; i ++) {
- JXHZoomImageView *imageView = [[JXHZoomImageView alloc] initWithTarget:self action:@selector(dismissView)];
- imageView.tag = spsb_imageViewTag + i;
- [_scrollView addSubview:imageView];
- [imageView makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.equalTo(i * (jxh_screenWidth() + spsb_offset));
- make.top.and.bottom.equalTo(0);
- make.size.equalTo((CGSize){jxh_screenWidth(), jxh_screenHeight()});
- }];
- }
-
- _pageControl = UIPageControl.new;
- _pageControl.hidesForSinglePage = true;
- [_bgView addSubview:_pageControl];
- [_pageControl makeConstraints:^(JXHConstraintMaker *make) {
- make.bottom.equalTo(-10);
- make.centerX.equalTo(self->_bgView);
- }];
- }
- @end
- #undef spsb_offset
- #undef spsb_imageViewTag
|