| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- // Copyright (c) 2019 Tencent. All rights reserved.
- #import "UGCKitImageScrollerViewController.h"
- @interface _UGCKitImageScrollerCellView : UICollectionViewCell
- @property (readonly, nonatomic) UIImageView *imageView;
- @property (readonly, nonatomic) UIButton *closeButton;
- @end
- static const CGFloat CellSize = 70;
- static const CGFloat CloseButtonSize = 20;
- typedef _UGCKitImageScrollerCellView CellClass;
- @interface UGCKitImageScrollerViewController () <UICollectionViewDelegateFlowLayout>
- {
- NSMutableArray *_assets;
- }
- @property (strong, nonatomic) PHCachingImageManager *imageManager;
- @end
- @implementation UGCKitImageScrollerViewController
- static NSString * const reuseIdentifier = @"Cell";
- - (instancetype)initWithImageManage:(PHCachingImageManager *)imageManager
- {
- UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
- flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- flowLayout.minimumInteritemSpacing = 2;
- flowLayout.sectionInset = UIEdgeInsetsMake(0, 8, 0, 8);
- if (self = [self initWithCollectionViewLayout:flowLayout]) {
- _imageManager = imageManager;
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- _assets = [[NSMutableArray alloc] init];
- UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
- [self.collectionView addGestureRecognizer:longGesture];
- [self.collectionView registerClass:[CellClass class] forCellWithReuseIdentifier:reuseIdentifier];
- }
- #pragma mark - Public
- - (void)addAsset:(PHAsset *)asset {
- [_assets addObject:asset];
- [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:_assets.count-1 inSection:0]]];
- }
- - (void)removeAssetAtIndex:(NSUInteger)index
- {
- [_assets removeObjectAtIndex:index];
- [self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]];
- }
- #pragma mark <UICollectionViewDataSource>
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 1;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return _assets.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- CellClass *cell = (CellClass*)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
- PHAsset *asset = _assets[indexPath.item];
- [cell.closeButton setImage:self.closeIcon forState:UIControlStateNormal];
- if (cell.closeButton.allTargets.count == 0) {
- [cell.closeButton addTarget:self action:@selector(onRemoveItem:) forControlEvents:UIControlEventTouchUpInside];
- }
- cell.tag = indexPath.item;
- [self.imageManager requestImageForAsset:asset
- targetSize:cell.imageView.bounds.size
- contentMode:PHImageContentModeAspectFill
- options:nil
- resultHandler:^(UIImage *result, NSDictionary *info) {
- if (cell.tag == indexPath.item) {
- cell.imageView.image = result;
- }
- }];
- return cell;
- }
- #pragma mark <UICollectionViewDelegate>
- - (CGSize)collectionView:(UICollectionView *)collectionView
- layout:(UICollectionViewLayout *)collectionViewLayout
- sizeForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- return CGSizeMake(CellSize+CloseButtonSize/2, CellSize+CloseButtonSize/2);
- }
- - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
- {
- return YES;
- }
- - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
- {
- PHAsset *asset = _assets[sourceIndexPath.item];
- [_assets removeObjectAtIndex:sourceIndexPath.item];
- [_assets insertObject:asset atIndex:destinationIndexPath.item];
- }
- #pragma mark - Actions
- - (void)onRemoveItem:(UIButton *)button
- {
- CellClass *cell = (CellClass *)button.superview.superview;
- NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
- if (indexPath) {
- NSUInteger index = indexPath.item;
- [self removeAssetAtIndex:index];
- if (self.onRemoveHandler) {
- self.onRemoveHandler(index);
- }
- }
- }
- - (void)onLongPress:(UILongPressGestureRecognizer *)sender {
- CGPoint location = [sender locationInView:self.collectionView];
- switch (sender.state) {
- case UIGestureRecognizerStateBegan: {
- NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:location];
- [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
- } break;
- case UIGestureRecognizerStateChanged:
- [self.collectionView updateInteractiveMovementTargetPosition:location];
- break;
- case UIGestureRecognizerStateEnded:
- [self.collectionView endInteractiveMovement];
- break;
- default:
- [self.collectionView cancelInteractiveMovement];
- break;
- }
- }
- @end
- @implementation _UGCKitImageScrollerCellView
- - (instancetype)initWithCoder:(NSCoder *)coder
- {
- self = [super initWithCoder:coder];
- if (self) {
- [self commonInit];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self commonInit];
- }
- return self;
- }
- - (void)commonInit {
- _imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
- _imageView.contentMode = UIViewContentModeScaleAspectFill;
- _imageView.clipsToBounds = YES;
- _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [self.contentView addSubview:_imageView];
- [self.contentView addSubview:_closeButton];
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- _imageView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(CloseButtonSize/2, 0, 0, CloseButtonSize/2));
- _closeButton.frame = CGRectMake(0, 0, CloseButtonSize, CloseButtonSize);
- _closeButton.center = CGPointMake(CGRectGetMaxX(_imageView.frame), CGRectGetMinY(_imageView.frame));
- }
- @end
|