[iOS点击单个cell高度的动画]

效果

效果.gif

说明

  1. 点击单个cell的时候,其展开与缩放动画实现起来是很麻烦的,做过相关需求的朋友一定知道其中的坑

实现思路
首先将cell的高度 封装到对应的模型中,当触发事件的时候修改模型的高度值,然后刷新对应的动画即可

下面上代码,首先是模型

@interface Model : NSObject

@property (nonatomic) CGFloat  normalHeight;
@property (nonatomic) CGFloat  expendHeight;
@property (nonatomic) BOOL     expend;

+ (instancetype)ModelWithNormalHeight:(CGFloat)normalHeight expendHeight:(CGFloat)expendHeight expend:(BOOL)expend;

@end

#import "Model.h"

@implementation Model

+ (instancetype)ModelWithNormalHeight:(CGFloat)normalHeight expendHeight:(CGFloat)expendHeight expend:(BOOL)expend {

    Model *model = [[Model alloc] init];
    
    model.normalHeight = normalHeight;
    model.expendHeight = expendHeight;
    model.expend       = expend;
    
    return model;
}

@end

Cell

#import <UIKit/UIKit.h>
#import "Model.h"

@interface InfoCell : UITableViewCell

@property (nonatomic, weak) NSIndexPath  *indexPath;
@property (nonatomic, weak) UITableView  *tableView;

- (void)loadData:(id)data;

@end

#import "InfoCell.h"

@interface InfoCell ()

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, weak)   Model    *model;
@property (nonatomic, strong) UIView   *lineView;
@property (nonatomic, strong) UILabel  *infoLabel;

@end

@implementation InfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setup];
    }
    
    return self;
}

- (void)setup {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    [self.button addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.button];
    
    self.lineView                 = [[UIView alloc] initWithFrame:CGRectMake(0, 49.5, 320, 0.5f)];
    self.lineView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5f];
    [self addSubview:self.lineView];
    
    self.infoLabel      = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 50)];
    self.infoLabel.text = @"Demo";
    [self addSubview:self.infoLabel];
}

- (void)buttonEvent {
    
    if (self.model.expend == YES) {
        
        self.model.expend = NO;
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
        
        [self normalStateWithAnimated:YES];
        
    } else {
        
        self.model.expend = YES;
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    
        [self expendStateWithAnimated:YES];
    }
}

- (void)loadData:(id)data {
    
    self.model = data;
    
    if (self.model.expend == YES) {
        
        self.lineView.frame  = CGRectMake(0, 99.5f, 320, 0.5f);
        self.infoLabel.frame = CGRectMake(30, 0, 100, 50);
        
    } else {
    
        self.lineView.frame  = CGRectMake(0, 49.5, 320, 0.5f);
        self.infoLabel.frame = CGRectMake(10, 0, 100, 50);
    }
}

- (void)normalStateWithAnimated:(BOOL)animated {
    
    if (animated == YES) {
        
        [UIView animateWithDuration:0.35f animations:^{
            
            self.lineView.frame  = CGRectMake(0, 49.5, 320, 0.5f);
            self.infoLabel.frame = CGRectMake(10, 0, 100, 50);
        }];
        
    } else {
    
        self.lineView.frame  = CGRectMake(0, 49.5, 320, 0.5f);
        self.infoLabel.frame = CGRectMake(10, 0, 100, 50);
    }
}

- (void)expendStateWithAnimated:(BOOL)animated {
    
    if (animated == YES) {
        
        [UIView animateWithDuration:0.35f animations:^{
            
            self.lineView.frame  = CGRectMake(0, 99.5f, 320, 0.5f);
            self.infoLabel.frame = CGRectMake(30, 0, 100, 50);
        }];
        
    } else {
        
        self.lineView.frame  = CGRectMake(0, 99.5f, 320, 0.5f);
        self.infoLabel.frame = CGRectMake(30, 0, 100, 50);
    }
}

@end

控制器

#import "ViewController.h"
#import "InfoCell.h"
#import "Model.h"

#define INFO_CELL @"INFO_CELL"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *datasArray;
@property (nonatomic, strong) UITableView    *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.datasArray = [NSMutableArray array];
    [self.datasArray addObject:[Model ModelWithNormalHeight:50.f expendHeight:100.f expend:NO]];
    [self.datasArray addObject:[Model ModelWithNormalHeight:50.f expendHeight:100.f expend:YES]];
    [self.datasArray addObject:[Model ModelWithNormalHeight:50.f expendHeight:100.f expend:YES]];
    [self.datasArray addObject:[Model ModelWithNormalHeight:50.f expendHeight:100.f expend:YES]];
    [self.datasArray addObject:[Model ModelWithNormalHeight:50.f expendHeight:100.f expend:YES]];
    [self.datasArray addObject:[Model ModelWithNormalHeight:50.f expendHeight:100.f expend:YES]];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate       = self;
    self.tableView.dataSource     = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[InfoCell class] forCellReuseIdentifier:INFO_CELL];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _datasArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:INFO_CELL];
    cell.indexPath = indexPath;
    cell.tableView = tableView;
    
    [cell loadData:_datasArray[indexPath.row]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    Model *model = _datasArray[indexPath.row];
    
    if (model.expend) {
        
        return model.expendHeight;
        
    } else {
    
        return model.normalHeight;
    }
}

@end

关键代码

关键.png

总结: 动画的关键是如下几句,在触发事件的时候 执行对应的动画即可,但前提是你要修改模型的高度,不然也看不到变化的

        [self.tableView beginUpdates];
        [self.tableView endUpdates];
        //像下面这样,修改模型高度或给动画 都是可以的
        [self normalStateWithAnimated:YES];
让我们来重点关注这行代码:[tableView beginUpdates];
文档中对这行代码的解释为让TableView产生插入,删除或重新加载cell
看到这里大家应该就恍然大悟了吧?原来当我们点击了一个cell后我们相当于重新加载了一遍我们的tableview,但是却和[tableView reloadata]是完全不一样的风格,reloadData这个方法会让tableView整体重新加载,相当于是作用在tableView上,而beginUpdates只是作用在cell上!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,276评论 4 61
  • 修行者也许没有那么丰富的学识,但是他们修行,所以就直接去了。 没错,因为他们没有太多的资讯,所以他们在路上会遇到...
    半尘道人阅读 337评论 0 0
  • 01 在我爸妈那个年代,婚姻是件不太复杂的事情。那个时候交通不发达,农村有辆自行车算不错了,婚姻大事也只能在自己周...
    苏大兄dei阅读 372评论 9 7
  • 对于URL转码问题的理解:URL转码分为编码和解码两个过程。 URL编码的理解: 首先:我们需要想想URL作为一个...
    zjlearn阅读 1,729评论 0 0
  • 玲妮今天太累,下班足足多半个钟,她厌烦加班,每次加班就令她烦燥得要命。本来每天按钟按点回家她都烦,何况加班。这个点...
    码字好玩儿阅读 630评论 5 1