self.collectionview.pagingEnabled = YES;
//1.CollectionView 添加长按手势
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longHandle:)];
[ self.collectionview addGestureRecognizer:longTap];
}
//2.长按方法
-(void)longHandle:(UILongPressGestureRecognizer *)gesture
{
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
{
NSIndexPath *indexPath = [self.collectionview indexPathForItemAtPoint:[gesture locationInView:self.collectionview]];
if (indexPath == nil) {
break;
}
[self.collectionview beginInteractiveMovementForItemAtIndexPath:indexPath];
//cell.layer添加抖动手势
for (UICollectionViewCell *cell in [self.collectionview visibleCells]) {
[self starShake:cell];
}
break;
}
case UIGestureRecognizerStateChanged:
{
[self.collectionview updateInteractiveMovementTargetPosition:[gesture locationInView:self.collectionview]];
break;
}
case UIGestureRecognizerStateEnded:
{
[self.collectionview endInteractiveMovement];
//cell.layer移除抖动手势
for (UICollectionViewCell *cell in [self.collectionview visibleCells]) {
[self stopShake:cell];
}
break;
}
default:
[self.collectionview cancelInteractiveMovement];
break;
}
}
//抖动动画
- (void)starShake:(UICollectionViewCell*)cell{
CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimation animation];
keyAnimaion.keyPath = @"transform.rotation";
keyAnimaion.values = @[@(-3 / 180.0 * M_PI),@(3 /180.0 * M_PI),@(-3/ 180.0 * M_PI)];//度数转弧度
keyAnimaion.removedOnCompletion = NO;
keyAnimaion.fillMode = kCAFillModeForwards;
keyAnimaion.duration = 0.3;
keyAnimaion.repeatCount = MAXFLOAT;
[cell.layer addAnimation:keyAnimaion forKey:@"cellShake"];
}
- (void)stopShake:(UICollectionViewCell*)cell{
[cell.layer removeAnimationForKey:@"cellShake"];
}