1、動畫cell
針對cell的動畫,在Delegate中對cell的layer進行操作:
2、實現代碼
#import "ViewController.h" #import "TableViewCell.h" #define CScreenWidth [[UIScreen mainScreen] bounds].size.width #define CScreenHeight [[UIScreen mainScreen] bounds].size.height @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *tableView; @property(nonatomic, strong)NSMutableArray *dataArr; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _dataArr = [NSMutableArray array]; for (int i=0; i<100; i++) { [_dataArr addObject:[NSString stringWithFormat:@"%d%d",i,i]]; } [_tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _dataArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; cell.label.text = _dataArr[indexPath.row]; return cell; } //快照截圖 //- (nullable UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0); // 將rect由rect所在視圖轉換到目標視圖view中,返回在目標視圖view中的rect //- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view; // 將rect從view中轉換到當前視圖中,返回在當前視圖中的rect //- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view; - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; UITableViewCell * selectedCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; UIView *clipView = [selectedCell snapshotViewAfterScreenUpdates:NO];//快照 CGRect clipFrame =[selectedCell convertRect:selectedCell.bounds toView:self.view]; NSLog(@"==%@",NSStringFromCGRect(clipFrame)); clipView.frame = clipFrame; UIView *view = [[UIView alloc] initWithFrame:clipFrame]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; [self.view addSubview:clipView]; [UIView animateWithDuration:1 animations:^{ view.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.3]; view.transform = CGAffineTransformMakeScale(1, CScreenHeight/clipFrame.size.height*2); } completion:^(BOOL finished) { [clipView removeFromSuperview]; [view removeFromSuperview]; }]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 50; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ CATransform3D transA = CATransform3DIdentity; transA.m34 = -1/500; transA = CATransform3DRotate(transA, M_PI_2, 1, 1, 0); cell.layer.transform = CATransform3DScale(transA, 0.1, 0.1, 1);; [UIView animateWithDuration:0.5 animations:^{ cell.layer.transform = CATransform3DIdentity; cell.layer.cornerRadius = 10; cell.layer.masksToBounds = YES; }]; } @end
3、附件:TableView的Delegate和DataSource的方法簡介(iOS11.0)

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> @optional // cell、區頭、區尾將要呈現和已經呈現 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0); - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); - (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); // cell、區頭、區尾高度設置 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; // cell、區頭、區尾高度估算(heightForRowAtIndexPath獲取cell會死循環) - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0); - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0); - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0); // 區頭、區尾自定義view - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // 設置附件類型、附件被點擊方法 - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED; - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath; // Selection // cell是否高亮、高亮和取消高亮調用函數 - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // 將要選擇、取消某個cell(適用於攔截點擊某個cell) - (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath; - (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0); // 已經選擇、取消某個cell(適用於cell事件處理) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0); // 設置cell的編輯樣式、刪除確定按鈕的標題文字 --左滑操作 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; - (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // 添加cell的編輯操作(就是多個操作按鈕)--左滑操作按鈕 - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED; // 取代上方editActionsForRowAtIndexPath方法 - (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos); // 創建操作按鈕UIContextualAction,UISwipeActionsConfiguration相當於封裝類 //UIContextualActionStyleNormal,UIContextualActionStyleDestructive(一直右滑直接刪除cell) - (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos); // 編輯時是否縮進 - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath; // 將要、已經結束編輯cell - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED; - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(nullable NSIndexPath *)indexPath __TVOS_PROHIBITED; // Moving/reordering // 移動特定的某行cell - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath; // 設置cell行縮進(可用於設置不同級別) - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies // 是否指定cell顯示菜單、彈出菜單方法、選擇菜單調用方法 - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0); - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0); - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0); // 能否獲得焦點、是否將要更新焦點、已經更新焦點、返回上一個焦點的NSIndexPath - (BOOL)tableView:(UITableView *)tableView canFocusRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0); - (BOOL)tableView:(UITableView *)tableView shouldUpdateFocusInContext:(UITableViewFocusUpdateContext *)context NS_AVAILABLE_IOS(9_0); - (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator NS_AVAILABLE_IOS(9_0); - (nullable NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView NS_AVAILABLE_IOS(9_0); // Spring Loading // cell是否支持iOS11 新特性 Drag and Drop (默認YES) - (BOOL)tableView:(UITableView *)tableView shouldSpringLoadRowAtIndexPath:(NSIndexPath *)indexPath withContext:(id<UISpringLoadedInteractionContext>)context API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos, watchos); @end

@protocol UITableViewDataSource<NSObject> @required //每個區返回多少行、初始化cell - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; @optional //返回多少區(默認一個區) - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented //設置區頭、區尾文字 - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; // Editing //cell是否能編輯、是否能移動 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; // 設置索引欄標題數組(右側的ABCD……)、設置索引欄標題對應的分區 - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView; - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // cell編輯時、移動時調用的方法 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath; @end