[iOS點(diǎn)擊單個(gè)cell高度的動(dòng)畫]

效果

效果.gif

說明

  1. 點(diǎn)擊單個(gè)cell的時(shí)候桐磁,其展開與縮放動(dòng)畫實(shí)現(xiàn)起來是很麻煩的驯用,做過相關(guān)需求的朋友一定知道其中的坑

實(shí)現(xiàn)思路
首先將cell的高度 封裝到對(duì)應(yīng)的模型中,當(dāng)觸發(fā)事件的時(shí)候修改模型的高度值,然后刷新對(duì)應(yīng)的動(dòng)畫即可

下面上代碼,首先是模型

@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

關(guān)鍵代碼

關(guān)鍵.png

總結(jié): 動(dòng)畫的關(guān)鍵是如下幾句,在觸發(fā)事件的時(shí)候 執(zhí)行對(duì)應(yīng)的動(dòng)畫即可,但前提是你要修改模型的高度,不然也看不到變化的

        [self.tableView beginUpdates];
        [self.tableView endUpdates];
        //像下面這樣,修改模型高度或給動(dòng)畫 都是可以的
        [self normalStateWithAnimated:YES];
讓我們來重點(diǎn)關(guān)注這行代碼:[tableView beginUpdates];
文檔中對(duì)這行代碼的解釋為讓TableView產(chǎn)生插入竞端,刪除或重新加載cell
看到這里大家應(yīng)該就恍然大悟了吧抡蛙?原來當(dāng)我們點(diǎn)擊了一個(gè)cell后我們相當(dāng)于重新加載了一遍我們的tableview茴肥,但是卻和[tableView reloadata]是完全不一樣的風(fēng)格肢娘,reloadData這個(gè)方法會(huì)讓tableView整體重新加載芭逝,相當(dāng)于是作用在tableView上塌碌,而beginUpdates只是作用在cell上!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末旬盯,一起剝皮案震驚了整個(gè)濱河市台妆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌胖翰,老刑警劉巖接剩,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異萨咳,居然都是意外死亡懊缺,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門培他,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鹃两,“玉大人,你說我怎么就攤上這事靶壮≌” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵腾降,是天一觀的道長拣度。 經(jīng)常有香客問我,道長螃壤,這世上最難降的妖魔是什么抗果? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮奸晴,結(jié)果婚禮上冤馏,老公的妹妹穿的比我還像新娘。我一直安慰自己寄啼,他們只是感情好逮光,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布代箭。 她就那樣靜靜地躺著,像睡著了一般涕刚。 火紅的嫁衣襯著肌膚如雪嗡综。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天杜漠,我揣著相機(jī)與錄音极景,去河邊找鬼。 笑死驾茴,一個(gè)胖子當(dāng)著我的面吹牛盼樟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锈至,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼晨缴,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了裹赴?” 一聲冷哼從身側(cè)響起喜庞,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎棋返,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雷猪,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡睛竣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了求摇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片射沟。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖与境,靈堂內(nèi)的尸體忽然破棺而出验夯,到底是詐尸還是另有隱情,我是刑警寧澤摔刁,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布挥转,位于F島的核電站,受9級(jí)特大地震影響共屈,放射性物質(zhì)發(fā)生泄漏绑谣。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一拗引、第九天 我趴在偏房一處隱蔽的房頂上張望借宵。 院中可真熱鬧,春花似錦矾削、人聲如沸壤玫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽欲间。三九已至择镇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間括改,已是汗流浹背腻豌。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留嘱能,地道東北人吝梅。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像惹骂,于是被迫代替她去往敵國和親苏携。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫对粪、插件右冻、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,103評(píng)論 4 62
  • 修行者也許沒有那么豐富的學(xué)識(shí),但是他們修行,所以就直接去了。 沒錯(cuò),因?yàn)樗麄儧]有太多的資訊,所以他們?cè)诼飞蠒?huì)遇到...
    半塵道人閱讀 322評(píng)論 0 0
  • 01 在我爸媽那個(gè)年代鄙币,婚姻是件不太復(fù)雜的事情肃叶。那個(gè)時(shí)候交通不發(fā)達(dá),農(nóng)村有輛自行車算不錯(cuò)了十嘿,婚姻大事也只能在自己周...
    蘇大兄dei閱讀 353評(píng)論 9 7
  • 對(duì)于URL轉(zhuǎn)碼問題的理解:URL轉(zhuǎn)碼分為編碼和解碼兩個(gè)過程因惭。 URL編碼的理解: 首先:我們需要想想U(xiǎn)RL作為一個(gè)...
    zjlearn閱讀 1,682評(píng)論 0 0
  • 玲妮今天太累,下班足足多半個(gè)鐘绩衷,她厭煩加班蹦魔,每次加班就令她煩燥得要命。本來每天按鐘按點(diǎn)回家她都煩唇聘,何況加班版姑。這個(gè)點(diǎn)...
    碼字好玩兒閱讀 582評(píng)論 5 1