標籤:des style blog class code java
效果:
長按某個Cell,Cell會有一個明顯的彈起放大效果。這時候,你可以通過拖動cell和其他Cell更換順序。
實現的原理:
1,浮層
長按後,UITableViewCell上會出現一個浮層,同時UITableViewCell隱藏。並且浮層可拖動。(對UITableViewCell產生一個快照)
1 #pragma mark - Helper methods 2 3 /** @brief Returns a customized snapshot of a given view. */ 4 - (UIView *)customSnapshoFromView:(UIView *)inputView { 5 UIView* snapshot = nil; 6 7 if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 7.0) { 8 //ios7.0 以下通過形式儲存快照 9 snapshot = [self customSnapShortFromViewEx:inputView];10 }else{11 //ios7.0 系統的快照方法12 snapshot = [inputView snapshotViewAfterScreenUpdates:YES];13 }14 15 snapshot.layer.masksToBounds = NO;16 snapshot.layer.cornerRadius = 0.0;17 snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);18 snapshot.layer.shadowRadius = 5.0;19 snapshot.layer.shadowOpacity = 0.4;20 21 return snapshot;22 }23 24 - (UIView *)customSnapShortFromViewEx:(UIView *)inputView25 {26 CGSize inSize = inputView.bounds.size;27 // 下面方法,第一個參數表示地區大小。第二個參數表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。第三個參數就是螢幕密度了28 UIGraphicsBeginImageContextWithOptions(inSize, NO, [UIScreen mainScreen].scale);29 [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];30 UIImage *image= UIGraphicsGetImageFromCurrentImageContext();31 UIGraphicsEndImageContext();32 UIImageView* snapshot = [[UIImageView alloc] initWithImage:image];33 34 return snapshot;35 }
2,更換某對UITableViewCell的位置
?
1 2 3 4 |
// ... update data source. [_items exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row]; // ... move the rows. [_tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath]; |
datasource裡面的data的位置替換,同時將tableView的Cell位置替換。
1 - (void)longPressGestureRecognized:(id)sender { 2 3 UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender; 4 UIGestureRecognizerState state = longPress.state; 5 6 CGPoint location = [longPress locationInView:_tableView]; 7 NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:location]; 8 9 static UIView *snapshot = nil; ///< A snapshot of the row user is moving.10 static NSIndexPath *sourceIndexPath = nil; ///< Initial index path, where gesture begins.11 12 switch (state) {13 case UIGestureRecognizerStateBegan: {14 if (indexPath) {15 sourceIndexPath = indexPath;16 17 UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];18 19 // Take a snapshot of the selected row using helper method.20 snapshot = [self customSnapshoFromView:cell];21 22 // Add the snapshot as subview, centered at cell‘s center...23 __block CGPoint center = cell.center;24 snapshot.center = center;25 snapshot.alpha = 0.0;26 [_tableView addSubview:snapshot];27 [UIView animateWithDuration:0.25 animations:^{28 29 // Offset for gesture location.30 center.y = location.y;31 snapshot.center = center;32 snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);33 snapshot.alpha = 0.98;34 35 cell.alpha = 0.0f;36 } completion:^(BOOL finished) {37 cell.hidden = YES;38 }];39 }40 break;41 }42 43 case UIGestureRecognizerStateChanged: {44 CGPoint center = snapshot.center;45 center.y = location.y;46 snapshot.center = center;47 48 // Is destination valid and is it different from source?49 if (indexPath && ![indexPath isEqual:sourceIndexPath]) {50 51 // ... update data source.52 [_items exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];53 54 // ... move the rows.55 [_tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];56 57 // ... and update source so it is in sync with UI changes.58 sourceIndexPath = indexPath;59 }60 break;61 }62 63 default: {64 // Clean up.65 UITableViewCell *cell = [_tableView cellForRowAtIndexPath:sourceIndexPath];66 [UIView animateWithDuration:0.25 animations:^{67 68 snapshot.center = cell.center;69 snapshot.transform = CGAffineTransformIdentity;70 snapshot.alpha = 0.0;71 72 cell.alpha = 1.0f;73 } completion:^(BOOL finished) {74 cell.hidden = NO;75 [snapshot removeFromSuperview];76 snapshot = nil;77 78 }];79 sourceIndexPath = nil;80 break;81 }82 }83 }
3, ......似乎沒有其他了吧?
你看,是不是很簡單呢!只需要上面幾行代碼,你就可以實現一個稍微有點好看的UITableViewCell變換位置的效果了。
下載Demo