美文网首页UIUITableVIewM_iOS
iOStableView的滑动动画

iOStableView的滑动动画

作者: 劉光軍_MVP | 来源:发表于2015-12-30 22:41 被阅读3555次
//
//  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

相关文章

  • iOStableView的滑动动画

  • 弹性滑动和惯性滑动

    弹性滑动指的是平滑的滑动(smoothScrollTo),可以用View动画,属性动画,Scroller来实现,当...

  • 七、JQuery-4、DIV的动画、显示及隐藏

    1、animate动画 2、slideUp、slideDown向上向下滑动 向上滑动 向下滑动 3、slideTo...

  • jQuery 制作无缝轮播的焦点图

    网站上经常见到焦点图动画,焦点图的动画有多种,如:淡入淡出型、上下滑动型、左右滑动型、左右滑动无缝型,对比用户体验...

  • View的滑动

    view的滑动方法有3种: scrollTo();scrollBy(); 动画(view动画和属性动画) 动态改变...

  • iOStableview联动

    首先你要创建两个tableview 然后初始化一下,最好是懒加载 左边的 tableView - (UITable...

  • animate

    jquery动画 显示与隐藏 淡入与淡出 滑动 停止动画 自定义动画 动画队列,将多组动画按顺序播放

  • TCP/IP 协议(转)

    参考:TCP/IP 协议tcp窗口滑动以及拥塞控制解析TCP之滑动窗口(动画演示)

  • View的滑动

    常见的View滑动方式: 通过View本身提供的scrollTo/scrollBy方法实现滑动 通过动画实现Vie...

  • 模仿高德滑动柱状图

    高德滑竿算路柱状图,支持展示未来时间的ETA信息查询,左右可滑动,带有动画效果,柱状可以放大做小,滑动会有惯性动画...

网友评论

本文标题:iOStableView的滑动动画

本文链接:https://www.haomeiwen.com/subject/ehiihttx.html