iOStableView的滑动动画

//
//  ViewController.m
//  TableViewCellScaleTestDemo
//
//  Created by 刘光军 on 15/12/30.
//  Copyright © 2015年 刘光军. All rights reserved.
//

#import "ViewController.h"
#import "UIView+YYAdd.h"

//cell
@interface MyCell : UITableViewCell

@property(nonatomic, strong) UILabel* label;/**< label */

-(void)setWithString:(NSString *)string;

@end


@implementation MyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
        imageView.image = [UIImage imageNamed:@"mew_interlaced"];
        imageView.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:imageView];
        
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(120, 10, self.frame.size.width - 120 - 20, 100)];
        _label.textColor = [UIColor colorWithWhite:0.7 alpha:1.0];
        self.label.font = [UIFont systemFontOfSize:20];
        [self.contentView addSubview:self.label];
    }
    return self;
}

- (void)setWithString:(NSString *)string {
    self.label.text = [NSString stringWithFormat:@"我是第%@行", string];
}

@end


//controller
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView* tableView;/**< tableView */
@property(nonatomic, strong) NSArray* arr;/**< array */

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.22 green:0.22 blue:0.22 alpha:1];
    _arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28", nil];
    [self configTabelView];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark -tableview
- (void)configTabelView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.22 green:0.22 blue:0.22 alpha:1];
    [self.view addSubview:self.tableView];
}

#pragma mark - 设置cell分割线靠右
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

#pragma mark - tableview代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    [cell setWithString:[_arr objectAtIndex:indexPath.row]];
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}
#pragma mark 设置scale
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat viewHeight = scrollView.height + scrollView.contentInset.top;
    for (MyCell *cell in [self.tableView visibleCells]) {
        CGFloat y = cell.centerY - scrollView.contentOffset.y;
        CGFloat p = y - viewHeight / 2;
        CGFloat scale = cos(p / viewHeight * 0.8) * 0.95;
        [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState animations:^{
            cell.contentView.transform = CGAffineTransformMakeScale(scale, scale);
        } completion:NULL];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中的tableView是一种用于展示大量数据的常见控件,而多级展开折叠则是一种实现在tableView中展示层级关系的常见需求,类似于QQ好友列表的效果。 实现这个效果的方法是通过tableView的数据源和代理方法来控制每个cell的样式和交互。以下是一个简单的实现步骤: 1. 设计数据模型:根据层级关系设计一个合适的数据模型,每个模型中包含展开折叠状态、子模型等相关属性。 2. 创建tableView:在界面上创建一个tableView,并设置其数据源和代理为当前控制器。 3. 实现数据源方法:在数据源方法中,根据模型的展开折叠状态来确定每个cell的样式和数量。 4. 实现代理方法:在代理方法中,根据用户的操作(点击或者滑动等)来更新模型的展开折叠状态,并刷新tableView。 5. 自定义cell:根据不同的展开折叠状态,自定义cell的样式和布局。 6. 添加动画效果:为了增加用户体验,在展开折叠的过程中可以添加一些动画效果,比如cell的展开和折叠、tableView滚动等。 7. 处理点击事件:根据用户的点击来判断是展开还是折叠,并更新对应的模型和tableView。 8. 优化性能:如果数据量较大,可以考虑使用懒加载或者异步加载等方式来减少内存占用和提高性能。 通过以上步骤,我们就可以实现类似QQ好友列表的多级展开折叠效果。当用户点击某个cell时,可以展开其子级内容,再次点击则折叠子级内容,以此类推,完成多级展开折叠的功能。 需要注意的是,实现这个效果需要根据具体的需求和设计来确定数据模型、点击事件和界面样式等方面的实现细节。以上是一个基本的实现思路,具体的实现过程可能会因具体的需求而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值