滑动cell的时候执行动画效果
效果图:
源码:
// // ViewController.m // AniTab // // Created by XianMingYou on 15/2/26. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import "ViewController.h" #import "ShowCell.h" @interface ViewController ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataSource; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化数据源 self.dataSource = [NSMutableArray new]; for (int i = 0; i < 40; i++) { [self.dataSource addObject:[NSString stringWithFormat:@"%02d YouXianMing", i]]; } // 初始化tableView self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.tableView registerClass:[ShowCell class] forCellReuseIdentifier:@"ShowCell"]; } #pragma mark - tableView代理 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ShowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShowCell"]; [cell accessData:self.dataSource[indexPath.row]]; return cell; } #pragma mark cell显示的时候 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ShowCell *showCell = (ShowCell *)cell; [showCell show]; } #pragma mark cell消失的时候 - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath { ShowCell *showCell = (ShowCell *)cell; [showCell hide]; } #pragma mark cell高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100; } @end
cell源码:
// // ShowCell.h // AniTab // // Created by XianMingYou on 15/2/26. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import <UIKit/UIKit.h> @interface ShowCell : UITableViewCell /** * 动画显示 */ - (void)show; /** * 动画隐藏 */ - (void)hide; /** * 处理数据 * * @param data 数据源 */ - (void)accessData:(id)data; @end
// // ShowCell.m // AniTab // // Created by XianMingYou on 15/2/26. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import "ShowCell.h" @interface ShowCellStoreValue : NSObject @property (nonatomic) CGRect startRect; @property (nonatomic) CGRect endRect; @end @implementation ShowCellStoreValue @end @interface ShowCell () @property (nonatomic, strong) UILabel *label; @property (nonatomic, strong) ShowCellStoreValue *storeValue; @end @implementation ShowCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 50)]; self.label.font = [UIFont italicSystemFontOfSize:30.f]; [self addSubview:self.label]; self.storeValue = [ShowCellStoreValue new]; self.storeValue.startRect = self.label.frame; self.storeValue.endRect = CGRectMake(10, 30 + 20, 300, 50); } return self; } - (void)accessData:(id)data { NSString *str = data; if ([str isKindOfClass:[NSString class]]) { self.label.text = str; } } /** * 动画显示 */ - (void)show { [UIView animateWithDuration:1.f animations:^{ self.label.frame = self.storeValue.endRect; }]; } - (void)hide { [self.label.layer removeAllAnimations]; self.label.frame = self.storeValue.startRect; } @end
原理: