iOS-一行代码让你的TableView动起来

最近看到的一个不错的demo,让tableView进入添加动画效果

UITableView 是iOS日常开发中经常使用到的控件。tableView的普通展示效果比较生硬,为了提升APP的活力,提升体验,我们可以对根据tableView的特点,操作Cell实现一些动画效果。

转自版主:https://juejin.im/post/59b11f606fb9a024a27c2a69

Github地址:https://github.com/alanwangmodify/TableViewAnimationKit

正文

一、效果展示:
这里写图片描述
这里写图片描述
这里写图片描述

等等总共十多种动画效果…我就不一一放上来了

二、使用方法

TableViewAnimationKit调用各个动画的方法都为类方法,只需一行代码就可以调用。
eg:

[TableViewAnimationKit shakeAnimationWithTableView:tableView];
TableViewAnimationKit提供的动画类方法

  • (void)moveAnimationWithTableView:(UITableView *)tableView;
  • (void)alphaAnimationWithTableView:(UITableView *)tableView;
  • (void)fallAnimationWithTableView:(UITableView *)tableView;
  • (void)shakeAnimationWithTableView:(UITableView *)tableView;
  • (void)overTurnAnimationWithTableView:(UITableView *)tableView;
  • (void)toTopAnimationWithTableView:(UITableView *)tableView;
  • (void)springListAnimationWithTableView:(UITableView *)tableView;
  • (void)shrinkToTopAnimationWithTableView:(UITableView *)tableView;
  • (void)layDonwAnimationWithTableView:(UITableView *)tableView;
  • (void)roteAnimationWithTableView:(UITableView *)tableView;
    三、源码讲解

先举其中一个动画效果为例子:
这里写图片描述

动画效果为Cell左右各自插入。
实现源代码很简单如下:
+ (void)shakeAnimationWithTableView:(UITableView *)tableView {

NSArray *cells = tableView.visibleCells;
for (int i = 0; i < cells.count; i++) {
    UITableViewCell *cell = [cells objectAtIndex:i];
    if (i%2 == 0) {
        cell.transform = CGAffineTransformMakeTranslation(-XS_SCREEN_WIDTH,0);
    }else {
        cell.transform = CGAffineTransformMakeTranslation(XS_SCREEN_WIDTH,0);
    }
    [UIView animateWithDuration:0.4 delay:i*0.03 usingSpringWithDamping:0.75 initialSpringVelocity:1/0.75 options:0 animations:^{
        cell.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {

    }];
}

}
主要思路为:
获得tableview的visibleCells数组,进行遍历,对每个执行动画,不同cell的执行时间、方向有所差异,一起构成整个动画。

帮版主推荐一下Github,有兴趣的帮他点个star呗~
也可以为我这个搬运工点个赞哦~谢谢��
Github地址:https://github.com/alanwangmodify/TableViewAnimationKit

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的iOS Tableview代码示例: 首先,在你的ViewController.h文件中添加UITableViewDelegate和UITableViewDataSource协议: ``` @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> ``` 然后,在你的ViewController.m文件中,初始化一个UITableView,并实现UITableViewDataSource和UITableViewDelegate协议的方法: ``` - (void)viewDidLoad { [super viewDidLoad]; UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; tableView.delegate = self; tableView.dataSource = self; [self.view addSubview:tableView]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; // 返回10行 } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = [NSString stringWithFormat:@"Row %ld", (long)indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSLog(@"Selected row %ld", (long)indexPath.row); } ``` 这个示例会在你的ViewController上创建一个UITableView,其中包含10行简单的文本。当你点击任何一行时,它会在控制台上打印所选行的索引。 希望这可以帮助你开始实现你自己的表视图!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值