给TableView添加动画及cell的自适应高度

弹出效果:

//给cell添加动画
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //设置Cell的动画效果为3D效果
    //设置x和y的初始值为0.1;
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    //x和y的最终值为1
    [UIView animateWithDuration:1 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
}

翻转效果:

 // 从锚点位置出发,逆时针绕 Y 和 Z 坐标轴旋转90度
    CATransform3D transform3D = CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 1.0);
    
    // 定义 cell 的初始状态
    cell.alpha = 0.0;
    cell.layer.transform = transform3D;
    cell.layer.anchorPoint = CGPointMake(0.0, 0.5); // 设置锚点位置;默认为中心点(0.5, 0.5)
    
    // 定义 cell 的最终状态,执行动画效果
    // 方式一:普通操作设置动画
    [UIView beginAnimations:@"transform" context:NULL];
    [UIView setAnimationDuration:0.5];
    cell.alpha = 1.0;
    cell.layer.transform = CATransform3DIdentity;
    CGRect rect = cell.frame;
    rect.origin.x = 0.0;
    cell.frame = rect;
    [UIView commitAnimations];

1.写一个获取lable高度的方法

-(NSAttributedString *)attributedBodyTextAtIndexPath:(NSIndexPath *)indexPath

{
    
    NSString *text = muarr[indexPath.row];
    
    UIFont *font = [UIFont systemFontOfSize:17];
    
    NSDictionary *testDic = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    
    NSAttributedString *string = [[NSAttributedString alloc]initWithString:text attributes:testDic];
    
    return string;
    
}

2.设置cell 高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // -20    左右距两边各为10;
    CGFloat labelWidth = self.tableview.bounds.size.width - 20;
    
    NSAttributedString *test = [self attributedBodyTextAtIndexPath:indexPath];
    
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading;
    
    CGRect rect = [test boundingRectWithSize:CGSizeMake(labelWidth, 0) options:options context:nil];
    
    return (CGFloat)(ceil(rect.size.height) + 20);
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,378评论 0 1
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • 1.xib方式创建 每个cell的显示的内容都是固定的,也就是cell的高度都是相同的 加载数据 有plist文件...
    WeiHing阅读 6,892评论 0 6
  • 5.21,晴,坐标杭州。 体测完,结束了多年来依旧让我痛不欲生的800m。跑过那么多次800,小学的操场,初中的操...
    安歌cc阅读 154评论 -1 1
  • 国庆七天长假,是人们出门旅行的最佳时期。本打算和张先生带着小孩儿回家,由于种种原因,出行的计划就此搁浅。 睁开惺忪...
    记忆悠悠阅读 508评论 6 2