小说绘上架版本
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// TFCollectionViewFlowLayout.h
|
||||
// TFPhotoBrowser
|
||||
//
|
||||
// Created by zhengwenming on 2018/6/11.
|
||||
// Copyright © 2018年 zhengwenming. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface TFCollectionViewFlowLayout : UICollectionViewFlowLayout
|
||||
|
||||
@property (nonatomic ,assign) CGFloat imgaeGap;
|
||||
|
||||
@end
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// TFCollectionViewFlowLayout.m
|
||||
// TFPhotoBrowser
|
||||
//
|
||||
// Created by zhengwenming on 2018/6/11.
|
||||
// Copyright © 2018年 zhengwenming. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TFCollectionViewFlowLayout.h"
|
||||
|
||||
@implementation TFCollectionViewFlowLayout
|
||||
|
||||
- (void)prepareLayout
|
||||
{
|
||||
[super prepareLayout];
|
||||
|
||||
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
|
||||
|
||||
CGSize size = self.collectionView.bounds.size;
|
||||
self.itemSize = CGSizeMake(size.width, size.height);
|
||||
self.minimumLineSpacing = 0;
|
||||
self.minimumInteritemSpacing = 10;
|
||||
}
|
||||
|
||||
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
|
||||
{
|
||||
NSArray<UICollectionViewLayoutAttributes *> *layoutAttsArray = [[NSArray alloc] initWithArray:[super layoutAttributesForElementsInRect:rect] copyItems:YES];
|
||||
|
||||
CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.bounds.size.width/2.0;
|
||||
|
||||
__block CGFloat min = CGFLOAT_MAX;
|
||||
__block NSUInteger minIdx;
|
||||
[layoutAttsArray enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger index, BOOL * _Nonnull stop) {
|
||||
if (ABS(centerX - obj.center.x) < min) {
|
||||
min = ABS(centerX - obj.center.x);
|
||||
minIdx = index;
|
||||
}
|
||||
}];
|
||||
|
||||
[layoutAttsArray enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger index, BOOL * _Nonnull stop) {
|
||||
if (minIdx - 1 == index) {
|
||||
obj.center = CGPointMake(obj.center.x - self.imgaeGap, obj.center.y);
|
||||
}
|
||||
if (minIdx + 1 == index) {
|
||||
obj.center = CGPointMake(obj.center.x + self.imgaeGap, obj.center.y);
|
||||
}
|
||||
}];
|
||||
return layoutAttsArray;
|
||||
}
|
||||
|
||||
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// TFPhotoBrowser.h
|
||||
// TFPhotoBrowser
|
||||
//
|
||||
// Created by zhengwenming on 2018/1/2.
|
||||
// Copyright © 2018年 zhengwenming. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
typedef void(^DeleteBlock)(NSMutableArray *dataSource, NSUInteger currentIndex, UICollectionView *collectionView);
|
||||
typedef void(^DownLoadBlock)(NSMutableArray *dataSource, UIImage *image, NSError *error);
|
||||
|
||||
@interface TFPhotoBrowser : UIViewController
|
||||
/**
|
||||
* 需要预览的照片数组
|
||||
*/
|
||||
@property (nonatomic ,strong) NSMutableArray *dataSource;
|
||||
|
||||
/**
|
||||
* 需要展示的当前的图片index
|
||||
*/
|
||||
@property (nonatomic ,assign) NSInteger currentPhotoIndex;
|
||||
|
||||
/**
|
||||
* 是否需要下载
|
||||
*/
|
||||
@property (nonatomic ,assign) BOOL downLoadNeeded;
|
||||
|
||||
/**
|
||||
* 是否需要删除
|
||||
*/
|
||||
@property (nonatomic ,assign) BOOL deleteNeeded;
|
||||
|
||||
/**
|
||||
* 下载回调
|
||||
*/
|
||||
@property (nonatomic ,copy) DownLoadBlock downLoadBlock;
|
||||
|
||||
/**
|
||||
* 删除回调
|
||||
*/
|
||||
@property (nonatomic ,copy) DeleteBlock deleteBlock;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
//
|
||||
// TFPhotoBrowser.m
|
||||
// TFPhotoBrowser
|
||||
//
|
||||
// Created by zhengwenming on 2018/1/2.
|
||||
// Copyright © 2018年 zhengwenming. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TFPhotoBrowser.h"
|
||||
#import "TFPhotoBrowserCell.h"
|
||||
#import "TFCollectionViewFlowLayout.h"
|
||||
|
||||
@interface TFPhotoBrowser ()<UICollectionViewDataSource ,UICollectionViewDelegate ,UIScrollViewDelegate>
|
||||
|
||||
@property(nonatomic ,assign) BOOL isHideNaviBar;
|
||||
@property(nonatomic ,strong) UICollectionView *collectionView;
|
||||
@property(nonatomic ,strong) UIPageControl *pageControl;
|
||||
@end
|
||||
|
||||
@implementation TFPhotoBrowser
|
||||
|
||||
- (BOOL)fullScreenGestureShouldBegin
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
// 是否支持自动转屏
|
||||
- (BOOL)shouldAutorotate
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
// 支持哪些屏幕方向
|
||||
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
|
||||
{
|
||||
return UIInterfaceOrientationMaskAllButUpsideDown;
|
||||
}
|
||||
|
||||
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
|
||||
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
|
||||
{
|
||||
return UIInterfaceOrientationPortrait;
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
if (@available(ios 11.0,*)) {
|
||||
// UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
||||
// UITableView.appearance.estimatedRowHeight = 0;
|
||||
// UITableView.appearance.estimatedSectionFooterHeight = 0;
|
||||
// UITableView.appearance.estimatedSectionHeaderHeight = 0;
|
||||
} else {
|
||||
if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
|
||||
self.automaticallyAdjustsScrollViewInsets = NO;
|
||||
}
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)viewWillDisappear:(BOOL)animated
|
||||
{
|
||||
[super viewWillDisappear:animated];
|
||||
|
||||
[self.navigationController setNavigationBarHidden:NO animated:YES];
|
||||
}
|
||||
|
||||
- (void)deleteTheImage:(UIBarButtonItem *)sender
|
||||
{
|
||||
if (self.dataSource.count == 1) {
|
||||
[self.dataSource removeObjectAtIndex:self.currentPhotoIndex];
|
||||
[self.navigationController popViewControllerAnimated:YES];
|
||||
|
||||
} else {
|
||||
[self.dataSource removeObjectAtIndex:self.currentPhotoIndex];
|
||||
|
||||
self.title = [NSString stringWithFormat:@"%@/%@", [TFUtilsHelper formatStringWithInteger:self.currentPhotoIndex + 1], [TFUtilsHelper formatStringWithInteger:self.dataSource.count]];
|
||||
|
||||
[self.collectionView reloadData];
|
||||
}
|
||||
|
||||
__weak typeof(self) weakSelf = self;
|
||||
if (self.deleteBlock) {
|
||||
self.deleteBlock(weakSelf.dataSource, weakSelf.currentPhotoIndex, weakSelf.collectionView);
|
||||
}
|
||||
}
|
||||
|
||||
- (UICollectionView *)collectionView
|
||||
{
|
||||
if (_collectionView == nil) {
|
||||
TFCollectionViewFlowLayout *layout = [[TFCollectionViewFlowLayout alloc] init];
|
||||
layout.imgaeGap = 20;
|
||||
|
||||
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
|
||||
_collectionView.backgroundColor = [UIColor blackColor];
|
||||
_collectionView.dataSource = self;
|
||||
_collectionView.delegate = self;
|
||||
_collectionView.pagingEnabled = YES;
|
||||
_collectionView.scrollsToTop = NO;
|
||||
[_collectionView registerClass:[TFPhotoBrowserCell class] forCellWithReuseIdentifier:@"TFPhotoBrowserCell"];
|
||||
_collectionView.showsHorizontalScrollIndicator = NO;
|
||||
_collectionView.contentOffset = CGPointMake(0, 0);
|
||||
_collectionView.contentSize = CGSizeMake(self.view.frame.size.width * self.dataSource.count, self.view.frame.size.height);
|
||||
}
|
||||
return _collectionView;
|
||||
}
|
||||
|
||||
// 视图发生了大小改变的时候会调用此方法 大小改变 == 横竖切换
|
||||
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
|
||||
{
|
||||
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
|
||||
|
||||
self.collectionView.frame = CGRectMake(0, 0, size.width, size.height);
|
||||
self.pageControl.frame = CGRectMake(0, size.height - 30, size.width, 30);
|
||||
self.pageControl.centerX = self.view.centerX;
|
||||
} else {
|
||||
self.collectionView.frame = CGRectMake(0, 0, size.width, size.height);
|
||||
self.pageControl.frame = CGRectMake(0, size.height - 30, size.width, 30);
|
||||
self.pageControl.centerX = self.view.centerX;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
[super viewDidLoad];
|
||||
|
||||
if (self.downLoadNeeded) {
|
||||
UIButton *saveImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
||||
saveImageBtn.frame = CGRectMake(0, 0, 40, 40);
|
||||
saveImageBtn.autoresizingMask = UIViewAutoresizingFlexibleHeight;
|
||||
[saveImageBtn setImage:[UIImage imageNamed:@"savePicture"] forState:UIControlStateNormal];
|
||||
[saveImageBtn setImage:[UIImage imageNamed:@"savePicture"] forState:UIControlStateHighlighted];
|
||||
[saveImageBtn addTarget:self action:@selector(saveImage) forControlEvents:UIControlEventTouchUpInside];
|
||||
|
||||
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveImageBtn];
|
||||
} else if(self.deleteNeeded) {
|
||||
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteTheImage:)];
|
||||
}
|
||||
|
||||
self.title = self.title ? self.title : [NSString stringWithFormat:@"%@/%@", [TFUtilsHelper formatStringWithInteger:self.currentPhotoIndex + 1], [TFUtilsHelper formatStringWithInteger:self.dataSource.count]];
|
||||
self.view.backgroundColor = [UIColor blackColor];
|
||||
self.isHideNaviBar = NO;
|
||||
[self.view addSubview:self.collectionView];
|
||||
|
||||
if (self.dataSource.count) {
|
||||
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(self.currentPhotoIndex) inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
|
||||
}
|
||||
[self.view addSubview:self.pageControl];
|
||||
self.pageControl.numberOfPages = self.dataSource.count;
|
||||
self.pageControl.currentPage = self.currentPhotoIndex;
|
||||
}
|
||||
|
||||
- (UIPageControl *)pageControl
|
||||
{
|
||||
if (!_pageControl) {
|
||||
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-30, self.view.frame.size.width, 30)];
|
||||
_pageControl.numberOfPages = 5;
|
||||
_pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
|
||||
_pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
|
||||
_pageControl.backgroundColor = [UIColor clearColor];
|
||||
}
|
||||
return _pageControl;
|
||||
}
|
||||
|
||||
- (void)saveImage
|
||||
{
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.currentPhotoIndex inSection:0];
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
TFPhotoBrowserCell *currentCell = (TFPhotoBrowserCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
|
||||
UIImageWriteToSavedPhotosAlbum(currentCell.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
|
||||
{
|
||||
if (error) {
|
||||
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"保存失败")];
|
||||
} else {
|
||||
[TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"保存成功")];
|
||||
}
|
||||
__weak typeof(self) weakSelf = self;
|
||||
if (self.downLoadBlock) {
|
||||
self.downLoadBlock(weakSelf.dataSource,image,error);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - UIScrollViewDelegate
|
||||
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
|
||||
// if (self.currentPhotoIndex==0) {
|
||||
// scrollView.bounces = NO;
|
||||
// }else{
|
||||
// scrollView.bounces = YES;
|
||||
// }
|
||||
}
|
||||
|
||||
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
|
||||
{
|
||||
if ([self.title isEqualToString:TFLocalizedString(@"图片预览")]) {
|
||||
|
||||
} else {
|
||||
CGPoint offSet = scrollView.contentOffset;
|
||||
self.currentPhotoIndex = offSet.x / self.view.width;
|
||||
self.title = [NSString stringWithFormat:@"%@/%@", [TFUtilsHelper formatStringWithInteger:self.currentPhotoIndex + 1], [TFUtilsHelper formatStringWithInteger:self.dataSource.count]];
|
||||
self.pageControl.currentPage = self.currentPhotoIndex;
|
||||
}
|
||||
}
|
||||
|
||||
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
|
||||
{
|
||||
return self.dataSource.count;
|
||||
}
|
||||
|
||||
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
TFPhotoBrowserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TFPhotoBrowserCell" forIndexPath:indexPath];
|
||||
cell.model = self.dataSource[indexPath.row];
|
||||
|
||||
WS(weakSelf)
|
||||
if (!cell.singleTapGestureBlock) {
|
||||
cell.singleTapGestureBlock = ^(){
|
||||
if (weakSelf.isHideNaviBar == YES) {
|
||||
[weakSelf.navigationController setNavigationBarHidden:NO animated:YES];
|
||||
} else {
|
||||
[weakSelf.navigationController setNavigationBarHidden:YES animated:YES];
|
||||
}
|
||||
weakSelf.isHideNaviBar = !weakSelf.isHideNaviBar;
|
||||
[weakSelf dismissViewControllerAnimated:YES completion:^{
|
||||
|
||||
}];
|
||||
};
|
||||
}
|
||||
|
||||
if (!cell.longPressGestureBlock) {
|
||||
|
||||
__weak typeof(self) weakSelf = self;
|
||||
cell.longPressGestureBlock = ^(TFPhotoBrowserCell *cell) {
|
||||
|
||||
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
|
||||
if (is_iPad) {
|
||||
UIPopoverPresentationController *popover = actionSheet.popoverPresentationController;
|
||||
|
||||
if (popover) {
|
||||
popover.sourceView = weakSelf.view;
|
||||
popover.sourceRect = weakSelf.view.bounds;
|
||||
|
||||
popover.permittedArrowDirections = UIPopoverArrowDirectionDown;
|
||||
}
|
||||
}
|
||||
[actionSheet addAction:[UIAlertAction actionWithTitle:TFLocalizedString(@"保存到相册") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
||||
UIImageWriteToSavedPhotosAlbum(cell.imageView.image, weakSelf,
|
||||
@selector(image:didFinishSavingWithError:contextInfo:), nil);
|
||||
}]];
|
||||
|
||||
[actionSheet addAction:[UIAlertAction actionWithTitle:TFLocalizedString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
|
||||
NSLog(@"取消");
|
||||
}]];
|
||||
[weakSelf presentViewController:actionSheet animated:YES completion:nil];
|
||||
};
|
||||
}
|
||||
cell.currentIndexPath = indexPath;
|
||||
|
||||
self.title = [NSString stringWithFormat:@"%@/%@", [TFUtilsHelper formatStringWithInteger:self.currentPhotoIndex + 1], [TFUtilsHelper formatStringWithInteger:self.dataSource.count]];
|
||||
|
||||
return cell;
|
||||
}
|
||||
|
||||
- (void)didReceiveMemoryWarning
|
||||
{
|
||||
[super didReceiveMemoryWarning];
|
||||
}
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// TFPhotoBrowserCell.h
|
||||
// TFPhotoBrowser
|
||||
//
|
||||
// Created by zhengwenming on 2018/1/2.
|
||||
// Copyright © 2018年 zhengwenming. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface TFPhotoBrowserCell : UICollectionViewCell
|
||||
|
||||
@property (nonatomic ,copy) NSIndexPath *currentIndexPath;
|
||||
@property (nonatomic ,strong) UIImageView *imageView;
|
||||
@property (nonatomic ,retain) id model;
|
||||
@property (nonatomic ,copy) void (^singleTapGestureBlock)(void);
|
||||
@property (nonatomic ,copy) void (^longPressGestureBlock)(TFPhotoBrowserCell *cell);
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,192 @@
|
||||
//
|
||||
// TFPhotoBrowserCell.m
|
||||
// TFPhotoBrowser
|
||||
//
|
||||
// Created by zhengwenming on 2018/1/2.
|
||||
// Copyright © 2018年 zhengwenming. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TFPhotoBrowserCell.h"
|
||||
|
||||
@interface TFPhotoBrowserCell ()<UIGestureRecognizerDelegate, UIScrollViewDelegate> {
|
||||
CGFloat _aspectRatio;
|
||||
}
|
||||
@property (nonatomic ,strong) UIScrollView *scrollView;
|
||||
@property (nonatomic ,strong) UIView *imageContainerView;
|
||||
@end
|
||||
|
||||
@implementation TFPhotoBrowserCell
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
{
|
||||
if (self = [super initWithFrame:frame]) {
|
||||
|
||||
self.backgroundColor = [UIColor blackColor];
|
||||
self.scrollView = [[UIScrollView alloc] init];
|
||||
self.scrollView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
|
||||
self.scrollView.bouncesZoom = YES;
|
||||
self.scrollView.backgroundColor = [UIColor blackColor];
|
||||
self.scrollView.maximumZoomScale = 2.5;//放大比例
|
||||
self.scrollView.minimumZoomScale = 1.0;//缩小比例
|
||||
self.scrollView.multipleTouchEnabled = YES;
|
||||
self.scrollView.delegate = self;
|
||||
self.scrollView.scrollsToTop = NO;
|
||||
self.scrollView.showsHorizontalScrollIndicator = NO;
|
||||
self.scrollView.showsVerticalScrollIndicator = NO;
|
||||
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
||||
self.scrollView.delaysContentTouches = NO;
|
||||
self.scrollView.canCancelContentTouches = YES;
|
||||
self.scrollView.alwaysBounceVertical = NO;
|
||||
[self.contentView addSubview:self.scrollView];
|
||||
|
||||
self.imageContainerView = [[UIView alloc] init];
|
||||
self.imageContainerView.clipsToBounds = YES;
|
||||
[self.scrollView addSubview:self.imageContainerView];
|
||||
|
||||
self.imageView = [[UIImageView alloc] init];
|
||||
self.imageView.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.500];
|
||||
self.imageView.clipsToBounds = YES;
|
||||
self.imageView.userInteractionEnabled = YES;
|
||||
[self.imageContainerView addSubview:self.imageView];
|
||||
|
||||
// 单击
|
||||
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
|
||||
[self.contentView addGestureRecognizer:singleTap];
|
||||
|
||||
// 双击
|
||||
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
|
||||
doubleTap.numberOfTapsRequired = 2;
|
||||
[singleTap requireGestureRecognizerToFail:doubleTap];
|
||||
[self.contentView addGestureRecognizer:doubleTap];
|
||||
|
||||
// 长按
|
||||
UILongPressGestureRecognizer *longPressGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToSavePhoto:)];
|
||||
[self.contentView addGestureRecognizer:longPressGes];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)longPressToSavePhoto:(UILongPressGestureRecognizer *)sender
|
||||
{
|
||||
if (sender.state == UIGestureRecognizerStateBegan) {
|
||||
if (self.longPressGestureBlock) {
|
||||
self.longPressGestureBlock(self);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)resizeSubviews
|
||||
{
|
||||
self.imageContainerView.origin = CGPointZero;
|
||||
self.imageContainerView.width = self.width;
|
||||
|
||||
UIImage *image = self.imageView.image;
|
||||
|
||||
if (image.size.height/image.size.width > self.height/self.width) {
|
||||
self.imageContainerView.height = floor(image.size.height / (image.size.width / self.width));
|
||||
} else {
|
||||
CGFloat height = image.size.height / image.size.width * self.width;
|
||||
if (height < 1 || isnan(height)) height = self.height;
|
||||
height = floor(height);
|
||||
|
||||
self.imageContainerView.height = height;
|
||||
self.imageContainerView.centerY = self.height / 2;
|
||||
}
|
||||
|
||||
if (self.imageContainerView.height > self.height && self.imageContainerView.height - self.height <= 1) {
|
||||
self.imageContainerView.height = self.height;
|
||||
}
|
||||
|
||||
self.scrollView.contentSize = CGSizeMake(self.width, MAX(self.imageContainerView.height, self.height));
|
||||
[self.scrollView scrollRectToVisible:self.bounds animated:NO];
|
||||
self.scrollView.alwaysBounceVertical = self.imageContainerView.height <= self.height ? NO : YES;
|
||||
self.imageView.frame = self.imageContainerView.bounds;
|
||||
}
|
||||
|
||||
- (void)layoutSubviews
|
||||
{
|
||||
[super layoutSubviews];
|
||||
[self resizeSubviews];
|
||||
}
|
||||
|
||||
- (void)doubleTap:(UITapGestureRecognizer *)tap
|
||||
{
|
||||
if (self.scrollView.zoomScale > 1.0) {
|
||||
[self.scrollView setZoomScale:1.0 animated:YES];
|
||||
} else {
|
||||
CGPoint touchPoint = [tap locationInView:self.imageView];
|
||||
CGFloat newZoomScale = self.scrollView.maximumZoomScale;
|
||||
CGFloat xsize = self.frame.size.width / newZoomScale;
|
||||
CGFloat ysize = self.frame.size.height / newZoomScale;
|
||||
|
||||
[self.scrollView zoomToRect:CGRectMake(touchPoint.x - xsize/2, touchPoint.y - ysize/2, xsize, ysize) animated:YES];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setModel:(id)model
|
||||
{
|
||||
_model = model;
|
||||
|
||||
[self.scrollView setZoomScale:1.0 animated:NO];
|
||||
|
||||
if ([model isKindOfClass:[UIImage class]]) {
|
||||
UIImage *aImage = (UIImage *)model;
|
||||
self.imageView.image = aImage;
|
||||
|
||||
} else if ([model isKindOfClass:[NSString class]]) {
|
||||
NSString *aString = (NSString *)model;
|
||||
if ([aString rangeOfString:@"http"].location != NSNotFound) {
|
||||
WS(weakSelf)
|
||||
[self.imageView setImageWithURL:[NSURL URLWithString:aString] placeholder:HoldImage options:YYWebImageOptionSetImageWithFadeAnimation completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
|
||||
[weakSelf resizeSubviews];
|
||||
}];
|
||||
} else {
|
||||
self.imageView.image = [UIImage imageNamed:aString];
|
||||
}
|
||||
} else if ([model isKindOfClass:[NSURL class]]) {
|
||||
NSURL *aURL = (NSURL *)model;
|
||||
WS(weakSelf)
|
||||
[self.imageView setImageWithURL:aURL placeholder:HoldImage options:YYWebImageOptionSetImageWithFadeAnimation completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
|
||||
[weakSelf resizeSubviews];
|
||||
}];
|
||||
}
|
||||
|
||||
[self resizeSubviews];
|
||||
}
|
||||
|
||||
- (void)singleTap:(UITapGestureRecognizer *)tap
|
||||
{
|
||||
if (self.singleTapGestureBlock) {
|
||||
self.singleTapGestureBlock();
|
||||
}
|
||||
}
|
||||
|
||||
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
|
||||
{
|
||||
return self.imageContainerView;
|
||||
}
|
||||
|
||||
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
|
||||
{
|
||||
CGFloat offsetX = (scrollView.width > scrollView.contentSize.width) ? (scrollView.width - scrollView.contentSize.width) * 0.5 : 0.0;
|
||||
CGFloat offsetY = (scrollView.height > scrollView.contentSize.height) ? (scrollView.height - scrollView.contentSize.height) * 0.5 : 0.0;
|
||||
self.imageContainerView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, scrollView.contentSize.height * 0.5 + offsetY);
|
||||
}
|
||||
|
||||
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
|
||||
{
|
||||
if (self.scrollView.contentOffset.x <= 0) {
|
||||
if ([otherGestureRecognizer.delegate isKindOfClass:NSClassFromString(@"_FDFullscreenPopGestureRecognizerDelegate")]) {
|
||||
return YES;
|
||||
}
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user