这是一个 UIGestureRecognizer 子类,用于实现 UICollectionView 的 Swipe-to-Select/Deselect 功能。
将 ARSwipeToSelectGestureRecognizer
添加到您的项目最简单的方法是通过 CocoaPods
pod 'ARSwipeToSelectGestureRecognizer'
或者您也可以将 Classes/
目录下的所有文件复制到您的项目中。请确保选中“将项目复制到目标文件夹”复选框。
#import "ARSwipeToSelectGestureRecognizer.h"
ARSwipeToSelectGestureRecognizer
实例,传递一个块来处理 NSIndexPath
切换- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView.allowsSelection = self.collectionView.allowsMultipleSelection = YES;
// Do any additional setup after loading the view.
ARSwipeToSelectGestureRecognizer *gestureRecognizer = [[ARSwipeToSelectGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:) toggleSelectedHandler:^(NSIndexPath *indexPath) {
if ([[self.collectionView indexPathsForSelectedItems] containsObject:indexPath]) {
[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
[self.collectionView cellForItemAtIndexPath:indexPath].alpha = 1.0;
} else {
[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
[self.collectionView cellForItemAtIndexPath:indexPath].alpha = 0.5;
}
}];
[self.collectionView addGestureRecognizer:gestureRecognizer];
}
ARSwipeToSelectGestureRecognizer
来选择多张照片以分共享,项目位于 Demo 文件夹中。