iOS 點(diǎn)擊段頭實(shí)現(xiàn)伸縮動(dòng)畫

原文Github地址:YouXianMing

gif效果

這個(gè)動(dòng)畫比較簡單少梁,主要思路為以下幾個(gè)步驟:

1.自定義TableView段頭并設(shè)置點(diǎn)擊事件

2.點(diǎn)擊后判斷是否展開矗钟,如果將要展開就插入cell恐仑;如果將要閉合就刪除cell

3.最后實(shí)現(xiàn)一些動(dòng)畫效果

以下是自定義UITableViewHeaderFooterView代碼
.h文件

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

@protocol RootHeaderFootViewDelegate <NSObject>

- (void)customHeaderFooterView;

@end

@interface RootHeaderFooterView : UITableViewHeaderFooterView
/** model */
@property (nonatomic, strong)HeaderModel *model;

@property (nonatomic, weak)UITableView *tableView;
@property (nonatomic, weak)UIViewController *viewController;
@property (nonatomic, weak)id <RootHeaderFootViewDelegate>delegate;
@property (nonatomic,assign) NSInteger section;
-(void)loadContent;
@end

.m文件

#import "RootHeaderFooterView.h"

@interface RootHeaderFooterView()
/**  */
@property (nonatomic, strong)UILabel *normalNameLabel;
/**  */
@property (nonatomic, strong)UILabel *highNameLabel;
/**  */
@property (nonatomic, strong)UIView *rotateView;

@end

@implementation RootHeaderFooterView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor orangeColor];
        self.model = [[HeaderModel alloc] init];
        self.userInteractionEnabled = YES;
        [self setupView];
    }
    return self;
}

- (void)setupView{
    self.normalNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 26)];
    self.normalNameLabel.font = [UIFont fontWithName:@"AppleSDGothicNeo" size:12];
    [self.contentView addSubview:self.normalNameLabel];
    
    self.highNameLabel = [[UILabel alloc] initWithFrame:self.normalNameLabel.frame];
    self.highNameLabel.font = self.normalNameLabel.font;
    self.highNameLabel.textColor = [UIColor redColor];
    [self.contentView addSubview:self.highNameLabel];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 400, 30)];
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn];
}

- (void)loadContent {
    self.normalNameLabel.text = self.model.className;
    self.highNameLabel.text = self.model.className;
    
    if (self.model.isOpen) {
        [self openTableAnimated];
    }else{
        [self closeTableAnimated];
    }
}

- (void)closeTableAnimated{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.normalNameLabel.alpha = 1;
        self.normalNameLabel.frame = CGRectMake(10 , 0, 100, 26);
        
        self.highNameLabel.alpha = 0;
        self.highNameLabel.frame = CGRectMake(10 , 0, 100, 26);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)openTableAnimated{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.normalNameLabel.alpha = 0;
        self.normalNameLabel.frame = CGRectMake(10 + 10, 0, 100, 26);
        
        self.highNameLabel.alpha = 1;
        self.highNameLabel.frame = CGRectMake(10 + 10, 0, 100, 26);
        self.contentView.backgroundColor = [UIColor whiteColor];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.contentView.backgroundColor = [UIColor orangeColor];
        } completion:nil];
        
    }];
}

- (void)clicked:(UIButton *)btn{
    
    if (self.model.isOpen) {
        self.model.isOpen = false;
        [self closeTableAnimated];
        NSMutableArray<NSIndexPath*> *indexPaths = [[NSMutableArray alloc] init];
        
        NSInteger index = self.model.students.count;
        for (NSInteger i = 0; i<index; i++) {
            NSIndexPath * indexpath = [NSIndexPath indexPathForItem:i inSection:self.section];
            [indexPaths addObject:indexpath];
            
        }
        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }else {
        self.model.isOpen = true;
        [self openTableAnimated];
        NSMutableArray<NSIndexPath*> *indexPaths = [[NSMutableArray alloc] init];
        
        NSInteger index = self.model.students.count;
        for (NSInteger i = 0; i<index; i++) {
            NSIndexPath * indexpath = [NSIndexPath indexPathForItem:i inSection:self.section];
            [indexPaths addObject:indexpath];
            
        }
        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }
}
@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坤溃,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌彤恶,老刑警劉巖钞钙,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異声离,居然都是意外死亡芒炼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門术徊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來本刽,“玉大人,你說我怎么就攤上這事弧关≈寻玻” “怎么了?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵世囊,是天一觀的道長别瞭。 經(jīng)常有香客問我,道長株憾,這世上最難降的妖魔是什么蝙寨? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮嗤瞎,結(jié)果婚禮上墙歪,老公的妹妹穿的比我還像新娘。我一直安慰自己贝奇,他們只是感情好虹菲,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著掉瞳,像睡著了一般毕源。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上陕习,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天霎褐,我揣著相機(jī)與錄音,去河邊找鬼该镣。 笑死冻璃,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的损合。 我是一名探鬼主播省艳,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼嫁审!你這毒婦竟也來了跋炕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤土居,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體擦耀,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡棉圈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了眷蜓。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片分瘾。...
    茶點(diǎn)故事閱讀 40,144評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖吁系,靈堂內(nèi)的尸體忽然破棺而出德召,到底是詐尸還是另有隱情,我是刑警寧澤汽纤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布上岗,位于F島的核電站,受9級特大地震影響蕴坪,放射性物質(zhì)發(fā)生泄漏肴掷。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一背传、第九天 我趴在偏房一處隱蔽的房頂上張望呆瞻。 院中可真熱鬧,春花似錦径玖、人聲如沸痴脾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赞赖。三九已至,卻和暖如春丰泊,著一層夾襖步出監(jiān)牢的瞬間薯定,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工瞳购, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留话侄,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓学赛,卻偏偏與公主長得像年堆,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子盏浇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評論 2 355

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