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 ()

@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

你可能感兴趣的:(iOStableView的滑动动画)