iOS仿網(wǎng)易考拉彈簧效果

先上網(wǎng)易考拉的效果


1536738568152694.gif

思路是下拉的同時(shí)改變header視圖的高度,主要代碼在scrollViewDidScroll代理方法中

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *myTableView;

@property (nonatomic, strong)UIView *headerContentView;

@property (nonatomic, strong)UIView *cardView;

@end

@implementation ViewController
{
    BOOL needAnimation;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createUI];
}

- (void)createUI {
    _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -220, self.view.frame.size.width, self.view.frame.size.height + 220)];
    _myTableView.backgroundColor = [UIColor whiteColor];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.tableHeaderView = [self createHeaderView];
    [self.view addSubview:_myTableView];
}

- (UIView *)createHeaderView {
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 420)];
    headerView.backgroundColor = [UIColor redColor];
    headerView.clipsToBounds = YES;
    
    _headerContentView = [[UIView alloc]initWithFrame:headerView.bounds];
    _headerContentView.backgroundColor = [UIColor redColor];
    [headerView addSubview:_headerContentView];
    
    _cardView = [[UIView alloc]initWithFrame:CGRectMake(15, 400, self.view.frame.size.width - 30, 200)];
    _cardView.backgroundColor = [UIColor blackColor];
    [_headerContentView addSubview:_cardView];
    
    UILabel *cardLabel = [[UILabel alloc]init];
    cardLabel.text = @"你看我叼嗎";
    cardLabel.textColor = [UIColor whiteColor];
    cardLabel.font = [UIFont boldSystemFontOfSize:20];
    [cardLabel sizeToFit];
    cardLabel.center = CGPointMake(_cardView.frame.size.width/2, _cardView.frame.size.height/2);
    [_cardView addSubview:cardLabel];
    
    return headerView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //NSLog(@"%lf", scrollView.contentOffset.y);
    CGFloat downHight = -scrollView.contentOffset.y;//下拉的距離
    CGRect frame = _headerContentView.frame;
    if (downHight < 0) {
        frame.size.height = 420;
        _headerContentView.frame = frame;
        _cardView.frame = CGRectMake(15, 400, self.view.frame.size.width - 30, 200);
    }else if (downHight < 100) {
        frame.size.height = 420 + downHight;
        _headerContentView.frame = frame;
        _cardView.frame = CGRectMake(15, 400 - downHight, self.view.frame.size.width - 30, 200);
    }else {
        frame.size.height = 420 + 100;
        _headerContentView.frame = frame;
        _cardView.frame = CGRectMake(15, 400 - 100, self.view.frame.size.width - 30, 200);

        _myTableView.contentOffset = CGPointMake(0, -100);//最多往下拉動(dòng)的距離篮愉,防止漏出白邊
    }
    
    if (scrollView.contentOffset.y < -20 && needAnimation) {
        [self runAnimation];//抖動(dòng)動(dòng)畫
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (scrollView.contentOffset.y < -20) {
        needAnimation = YES;
    }
}

- (void)runAnimation {
    needAnimation = NO;
    
    [UIView animateWithDuration:0.4 animations:^{
        [self.myTableView setContentOffset:CGPointMake(0, -20)];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:8 options:UIViewAnimationOptionCurveEaseOut animations:^{
            [self.myTableView setContentOffset:CGPointMake(0, -15)];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                [self.myTableView setContentOffset:CGPointMake(0, -20)];
            }];
        }];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

中間有個(gè)抖動(dòng)效果

[UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:8 options:UIViewAnimationOptionCurveEaseOut animations:^{
    [self.myTableView setContentOffset:CGPointMake(0, -15)];
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.3 animations:^{
        [self.myTableView setContentOffset:CGPointMake(0, -20)];
    }];
}];

最終效果


1536738569950076.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末诊沪,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子砌函,更是在濱河造成了極大的恐慌斩披,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件讹俊,死亡現(xiàn)場(chǎng)離奇詭異垦沉,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)仍劈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門厕倍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人贩疙,你說(shuō)我怎么就攤上這事讹弯】黾龋” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵组民,是天一觀的道長(zhǎng)棒仍。 經(jīng)常有香客問我,道長(zhǎng)邪乍,這世上最難降的妖魔是什么降狠? 我笑而不...
    開封第一講書人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮庇楞,結(jié)果婚禮上榜配,老公的妹妹穿的比我還像新娘。我一直安慰自己吕晌,他們只是感情好蛋褥,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著睛驳,像睡著了一般烙心。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上乏沸,一...
    開封第一講書人閱讀 51,562評(píng)論 1 305
  • 那天淫茵,我揣著相機(jī)與錄音,去河邊找鬼蹬跃。 笑死匙瘪,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蝶缀。 我是一名探鬼主播丹喻,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼翁都!你這毒婦竟也來(lái)了碍论?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤柄慰,失蹤者是張志新(化名)和其女友劉穎鳍悠,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體坐搔,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贼涩,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了薯蝎。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片遥倦。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出袒哥,到底是詐尸還是另有隱情缩筛,我是刑警寧澤,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布堡称,位于F島的核電站瞎抛,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏却紧。R本人自食惡果不足惜桐臊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望晓殊。 院中可真熱鬧断凶,春花似錦、人聲如沸巫俺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)介汹。三九已至却嗡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間嘹承,已是汗流浹背窗价。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留叹卷,地道東北人撼港。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像豪娜,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子哟楷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355