1,創建collectionview的時候添加長按手勢
cellPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellMoveingAction:)];
[self.collectionView addGestureRecognizer:cellPress];
手勢最好聲明為全局的,這樣在后面通過手勢定位cell,可以方便去修改每個cell上面的內容
2,定義cell的抖動動畫
//開始抖動動畫
-(void)beginMove{
for (SYCollectionViewCell * cell in [self.collectionView visibleCells]) {
cell.disButton.hidden = NO;
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAutoreverse animations:^{
cell.transform = CGAffineTransformMakeRotation(0.05);
} completion:nil];
}
}
//取消抖動動畫
-(void)endMove{
for (SYCollectionViewCell * cell in [self.collectionView visibleCells]) {
cell.disButton.hidden = YES;
[cell.layer removeAllAnimations];
}
}
3,處理長按手勢
-(void)cellMoveingAction:(UILongPressGestureRecognizer *)sender{
NSIndexPath * index = [self.collectionView indexPathForItemAtPoint:[sender locationInView:self.collectionView]];
switch (sender.state) {
case UIGestureRecognizerStateBegan://手勢開始
{
//開始在特定的索引路徑上對cell(單元)進行Interactive Movement(交互式移動工作
[self.collectionView beginInteractiveMovementForItemAtIndexPath:index];
[self beginMove];
}
break;
case UIGestureRecognizerStateChanged://手勢開始移動
{
//在手勢作用期間更新交互移動的目標位置。
[self.collectionView updateInteractiveMovementTargetPosition:[sender locationInView:self.collectionView]];
[self beginMove];
}
break;
case UIGestureRecognizerStateEnded://手勢結束
{
//在完成手勢動作后,結束交互式移動
[self.collectionView endInteractiveMovement];
[self endMove];
[self.collectionView reloadData];
}
break;
default:
{
//取消Interactive Movement。
[self.collectionView endInteractiveMovement];
}
break;
}
}
4,在collectionview的UICollectionViewDataSource方法里面交換數據源,然后刷新collectionview
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//這里不使用下面的語句是因為下面的語句是交換兩個數據源的位置
// [self.dataArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
//使用下面的方式,使動畫看起來更流暢,先移除要移動的數據,然后在要放置的位置插入數據
NSString * sourceStr = self.dataArray[sourceIndexPath.row];
[self.dataArray removeObject:self.dataArray[sourceIndexPath.row]];
[self.dataArray insertObject:sourceStr atIndex:destinationIndexPath.row];
[self.collectionView reloadData];
}
這里寫下view的幾種動畫模式
1.常規動畫屬性設置(可以同時選擇多個進行設置)
UIViewAnimationOptionLayoutSubviews:動畫過程中保證子視圖跟隨運動。
UIViewAnimationOptionAllowUserInteraction:動畫過程中允許用戶交互。
UIViewAnimationOptionBeginFromCurrentState:所有視圖從當前狀態開始運行。
UIViewAnimationOptionRepeat:重復運行動畫。
UIViewAnimationOptionAutoreverse :動畫運行到結束點后仍然以動畫方式回到初始點。
UIViewAnimationOptionOverrideInheritedDuration:忽略嵌套動畫時間設置。
UIViewAnimationOptionOverrideInheritedCurve:忽略嵌套動畫速度設置。
UIViewAnimationOptionAllowAnimatedContent:動畫過程中重繪視圖(注意僅僅適用于轉場動畫)。
UIViewAnimationOptionShowHideTransitionViews:視圖切換時直接隱藏舊視圖、顯示新視圖,而不是將舊視圖從父視圖移除(僅僅適用于轉場動畫)
UIViewAnimationOptionOverrideInheritedOptions :不繼承父動畫設置或動畫類型。
2.動畫速度控制(可從其中選擇一個設置)
UIViewAnimationOptionCurveEaseInOut:動畫先緩慢,然后逐漸加速。
UIViewAnimationOptionCurveEaseIn :動畫逐漸變慢。
UIViewAnimationOptionCurveEaseOut:動畫逐漸加速。
UIViewAnimationOptionCurveLinear :動畫勻速執行,默認值。
3.轉場類型(僅適用于轉場動畫設置,可以從中選擇一個進行設置,基本動畫、關鍵幀動畫不需要設置)
UIViewAnimationOptionTransitionNone:沒有轉場動畫效果。
UIViewAnimationOptionTransitionFlipFromLeft :從左側翻轉效果。
UIViewAnimationOptionTransitionFlipFromRight:從右側翻轉效果。
UIViewAnimationOptionTransitionCurlUp:向后翻頁的動畫過渡效果。
UIViewAnimationOptionTransitionCurlDown :向前翻頁的動畫過渡效果。
UIViewAnimationOptionTransitionCrossDissolve:舊視圖溶解消失顯示下一個新視圖的效果。
UIViewAnimationOptionTransitionFlipFromTop :從上方翻轉效果。
UIViewAnimationOptionTransitionFlipFromBottom:從底部翻轉效果。
注:這里是這個練習的demo