滑动cell的时候执行动画效果

滑动cell的时候执行动画效果

效果图:

源码:

//
//  ViewController.m
//  AniTab
//
//  Created by XianMingYou on 15/2/26.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "ViewController.h"
#import "ShowCell.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 初始化数据源
    self.dataSource = [NSMutableArray new];
    for (int i = 0; i < 40; i++) {
        [self.dataSource addObject:[NSString stringWithFormat:@"%02d YouXianMing", i]];
    }
    
    // 初始化tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                  style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[ShowCell class]
           forCellReuseIdentifier:@"ShowCell"];
}

#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShowCell"];
    [cell accessData:self.dataSource[indexPath.row]];
    
    return cell;
}

#pragma mark cell显示的时候
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    ShowCell *showCell = (ShowCell *)cell;
    [showCell show];
}

#pragma mark cell消失的时候
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
    ShowCell *showCell = (ShowCell *)cell;
    [showCell hide];
}

#pragma mark cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

@end

cell源码:
//
//  ShowCell.h
//  AniTab
//
//  Created by XianMingYou on 15/2/26.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ShowCell : UITableViewCell

/**
 *  动画显示
 */
- (void)show;

/**
 *  动画隐藏
 */
- (void)hide;

/**
 *  处理数据
 *
 *  @param data 数据源
 */
- (void)accessData:(id)data;

@end


//
//  ShowCell.m
//  AniTab
//
//  Created by XianMingYou on 15/2/26.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "ShowCell.h"

@interface ShowCellStoreValue : NSObject
@property (nonatomic) CGRect startRect;
@property (nonatomic) CGRect endRect;
@end
@implementation ShowCellStoreValue
@end

@interface ShowCell ()

@property (nonatomic, strong) UILabel            *label;
@property (nonatomic, strong) ShowCellStoreValue *storeValue;

@end

@implementation ShowCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 50)];
        self.label.font = [UIFont italicSystemFontOfSize:30.f];
        [self addSubview:self.label];

        self.storeValue = [ShowCellStoreValue new];
        self.storeValue.startRect = self.label.frame;
        self.storeValue.endRect   = CGRectMake(10, 30 + 20, 300, 50);
    }
    
    return self;
}

- (void)accessData:(id)data {
    NSString *str = data;
    if ([str isKindOfClass:[NSString class]]) {
        self.label.text = str;
    }
}

/**
 *  动画显示
 */
- (void)show {
    [UIView animateWithDuration:1.f animations:^{
        self.label.frame = self.storeValue.endRect;
    }];
}

- (void)hide {
    [self.label.layer removeAllAnimations];
    self.label.frame = self.storeValue.startRect;
}

@end

原理:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值