iOS自定义TableView收缩动画

Xcode下载解决办法:到该地址去手动下载安装器安装:
https://idmsa.apple.com/IDMSWebAuth/signin?appIdKey=891bd3417a7776362562d2197f89480a8547b108fd934911bcbea0110d07f757&path=%2Fdownload%2Fmore%2F&rv=1

//  SFMileageView.h

#import 

typedef void(^SFMileageViewBlock)(NSDictionary *);

@interface SFMileageView : UIView

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) SFMileageViewBlock mileageViewBlock;

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger )type mileageViewBlock:(SFMileageViewBlock)block;

- (void)titleLabelChangeFontWithIndex:(CGFloat)index;

@end


//  SFMileageView.m

#import "SFMileageView.h"

@interface SFMileageView ()

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, assign) NSInteger type;
@end

@implementation SFMileageView

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger )type mileageViewBlock:(SFMileageViewBlock)block{
    self = [super initWithFrame:frame];
    if (self) {
        self.type = type;
        self.mileageViewBlock = block;

        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:16.0];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.adjustsFontSizeToFitWidth = YES;
        self.titleLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.titleLabel];
        
        self.imageView = [[UIImageView alloc] init];
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.imageView.image = [UIImage imageNamed:@"clear_voice"];
        [self addSubview:self.imageView];
        
        self.userInteractionEnabled  = YES;
        self.imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mileageViewTap:)];
        [self addGestureRecognizer:tap];
        
        if (type == 0) {
            self.imageView.hidden =  YES;
            self.titleLabel.frame = CGRectMake(0, 5, frame.size.width - 10, frame.size.height - 5);
            self.imageView.frame = CGRectMake(frame.size.width - 25, -5, 30, 30);
        }else{
            self.imageView.hidden =  NO;
            self.titleLabel.frame = CGRectMake(20, 5, frame.size.width - 20, frame.size.height - 5);
            self.imageView.frame = CGRectMake(-5, 0, 30, 30);
        }
        
    }
    return self;
}

- (void)mileageViewTap:(UITapGestureRecognizer *)sender
{
    if (self.mileageViewBlock) {
        self.mileageViewBlock(nil);
    }
}

- (void)setTitle:(NSString *)title
{
    if (self.type == 0) {
        if (!title || [title floatValue] <= 0) {
            title = @"0";
        }
        _title = title;
        NSString * text = [NSString stringWithFormat:@"  %@  %@",title, LOCAL(@"公里")];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
        NSRange range1 = [text rangeOfString:title];
        NSRange range2 = [text rangeOfString:LOCAL(@"公里")];
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:24] range:range1];
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:14] range:range2];
        self.titleLabel.attributedText = str;
    }else{
        self.titleLabel.text = title;
        self.imageView.centerY = self.titleLabel.centerY;
    }
   
}

- (void)titleLabelChangeFontWithIndex:(CGFloat)index
{
    CGFloat point = 0.4;
    if (index > point) {
        self.titleLabel.alpha = 0 + (index-point)/(1 - point);
    }else{
        self.titleLabel.alpha =  0 + (point - index)/point;
    }
    self.imageView.alpha = self.titleLabel.alpha - 0;

    if (self.type == 0) {
        NSString * text = @"";
        if (index > point) {
            self.imageView.hidden = NO;
            self.titleLabel.textAlignment = NSTextAlignmentRight;
            text = self.title;
        }else{
            self.imageView.hidden = YES;
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            text = [NSString stringWithFormat:@"   %@  %@",self.title, LOCAL(@"公里")];
        }
        self.titleLabel.frame = CGRectMake(0, 5, self.frame.size.width - 20 * index, self.frame.size.height - 5);

        CGFloat font = 5 * index;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
        NSRange range1 = [text rangeOfString:self.title];
        NSRange range2 = [text rangeOfString:LOCAL(@"公里")];
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:48 - 4 *font] range:range1];
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:16] range:range2];
        self.titleLabel.attributedText = str;
    }else{

        if (index > point) {
            self.imageView.hidden = YES;
            self.titleLabel.textAlignment = NSTextAlignmentLeft;
            self.titleLabel.text = LOCAL(@"当月里程(公里)");
            self.titleLabel.frame = CGRectMake(0, 5, self.frame.size.width, self.frame.size.height - 5);
            self.titleLabel.font = [UIFont fontWithName:@"Arial" size:18];
        }else{
            self.imageView.hidden = NO;
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            self.titleLabel.text = LOCAL(@"里程数仅作为参考,不作为最终计薪的里程");
            self.titleLabel.frame = CGRectMake(20, 5, self.frame.size.width - 20, self.frame.size.height -20 + 15.0/point * index);
            self.titleLabel.font = [UIFont fontWithName:@"Arial" size:16 + index * (2.0/point)];
        }
        self.imageView.centerY = self.titleLabel.centerY;
    }
    
}


@end



#import 

@interface SFMileageViewController : UIViewController

@end


@interface SFTableView : UITableView


//  SFMileageViewController.m


#import "SFMileageViewController.h"
#import "UIImage+SFGif.h"
#import "SFMileageView.h"

#define BottomWidth (250)

@interface SFMileageViewController ()
@property (nonatomic, strong) SFTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property(nonatomic, strong)UIView *headView;

@property(nonatomic, strong)UIView *headTitleView;
@property(nonatomic, strong)UILabel *topTestLabel;
@property(nonatomic, strong)UILabel *bottomBtnLabel;

@property(nonatomic, strong)SFMileageView *centerTopView;
@property(nonatomic, strong)SFMileageView *centerBottomView;

@end

@implementation SFMileageViewController
#pragma mark - ViewCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = LOCAL(@"我的里程");
    [self.view addSubview:self.headView];

    [self initTabView];
    
    if (self.dataArr.count == 0) {
        self.tableView.mj_footer.hidden = YES;
    }

}

#pragma mark--- CustomView
- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
        _headView.backgroundColor = UIMainColor;
        [_headView addSubview:self.headTitleView];
    }
    return _headView;
}

- (UIView *)headTitleView{
    if (!_headTitleView) {
        _headTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _headView.frame.size.height-0)];
        _headTitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _headTitleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
        
        _centerTopView = [[SFMileageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-60, 40, 120, 40) type:0 mileageViewBlock:^(NSDictionary * dict) {
            [SVProgressHUD showInfoWithStatus:@"展示里程说明弹窗"];
        }];
        [_headTitleView addSubview:_centerTopView];
        _centerTopView.title  = @"288";
        [_centerTopView titleLabelChangeFontWithIndex:0];
        
        _centerBottomView = [[SFMileageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - BottomWidth)/2, 80, BottomWidth, 40) type:1 mileageViewBlock:nil];
        [_headTitleView addSubview:_centerBottomView];
        _centerBottomView.title  = LOCAL(@"里程数仅作为参考,不作为最终计薪的里程");
        [_centerBottomView titleLabelChangeFontWithIndex:0];
    }
    return _headTitleView;
}

#pragma mark----UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat offSetY = scrollView.contentOffset.y;
//    NSLog(@"===%f",offSetY);

    if (scrollView.tag==1000) {
        
        if (offSetY>=120) {
            scrollView.contentOffset = CGPointMake(0, offSetY);
            
            if (_headView.frame.size.height != 60) {
                NSInteger index = 1;
                [UIView animateWithDuration:0.05 animations:^{
                    CGFloat width = SCREEN_WIDTH/2-70;
                    _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 120-60*(index));
                    _centerTopView.frame = CGRectMake(SCREEN_WIDTH/2-60+ width*index, 40-30*index, 120, 40);
                    [_centerTopView titleLabelChangeFontWithIndex:index];
                    CGFloat width1 = (SCREEN_WIDTH - BottomWidth)/2 - 10;
                    CGFloat width2 = (SCREEN_WIDTH - BottomWidth)/2;
                    _centerBottomView.frame = CGRectMake(width2 - width1*index, 80-70*index, BottomWidth, 40);
                    [_centerBottomView titleLabelChangeFontWithIndex:index];
                }];

            }
          
        }else if(offSetY>-0){

            CGFloat index = (offSetY+0)/120;
            [UIView animateWithDuration:0.05 animations:^{
                CGFloat width = SCREEN_WIDTH/2-70;
                _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 120-60*(index));
                _centerTopView.frame = CGRectMake(SCREEN_WIDTH/2-60+ width*index, 40-30*index, 120, 40);
                [_centerTopView titleLabelChangeFontWithIndex:index];
                CGFloat width1 = (SCREEN_WIDTH - BottomWidth)/2 - 10;
                CGFloat width2 = (SCREEN_WIDTH - BottomWidth)/2;
                _centerBottomView.frame = CGRectMake(width2 - width1*index, 80-70*index, BottomWidth, 40);
                [_centerBottomView titleLabelChangeFontWithIndex:index];
            }];
        }else {
            if (_headView.frame.size.height != 120) {
                NSInteger index = 0;
                [UIView animateWithDuration:0.05 animations:^{
                    CGFloat width = SCREEN_WIDTH/2-70;
                    _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 120-60*(index));
                    _centerTopView.frame = CGRectMake(SCREEN_WIDTH/2-60+ width*index, 40-30*index, 120, 40);
                    [_centerTopView titleLabelChangeFontWithIndex:index];
                    CGFloat width1 = (SCREEN_WIDTH - BottomWidth)/2 - 10;
                    CGFloat width2 = (SCREEN_WIDTH - BottomWidth)/2;
                    _centerBottomView.frame = CGRectMake(width2 - width1*index, 80-70*index, BottomWidth, 40);
                    [_centerBottomView titleLabelChangeFontWithIndex:index];
                }];

            }
        }
        
    }
}



- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    CGFloat offSetY = scrollView.contentOffset.y;
    NSLog(@"111111111111:%.2f",scrollView.contentOffset.y);

    [UIView animateWithDuration:0.05 animations:^{
        if (offSetY > 50 && offSetY <= 120) {
            [scrollView setContentOffset:CGPointMake(0, 120) animated:YES];
        }
        
        if (offSetY > 0 && offSetY <= 50) {
            [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }
    }];
   
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
//    CGFloat offSetY = scrollView.contentOffset.y;
//    if (offSetY > 30 && offSetY <= 120) {
//        [scrollView setContentOffset:CGPointMake(0, 120) animated:YES];
//    }
////    NSLog(@"offSetY---%.2f",offSetY);
//    if (offSetY > -1120 && offSetY <= 30) {
//        [scrollView setContentOffset:CGPointMake(0, -0) animated:YES];
//    }
    NSLog(@"2222222222222:%.2f",scrollView.contentOffset.y);

}


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
{
    NSLog(@"333333333333333:%.2f",scrollView.contentOffset.y);
//    CGFloat offSetY = scrollView.contentOffset.y;
//    if (offSetY > 30 && offSetY <= 120) {
//        [scrollView setContentOffset:CGPointMake(0, 120) animated:YES];
//    }
//    if (offSetY > -1120 && offSetY <= 30) {
//        [scrollView setContentOffset:CGPointMake(0, -0) animated:YES];
//    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
    CGFloat offSetY = scrollView.contentOffset.y;

    NSLog(@"44444444444444:%.2f",scrollView.contentOffset.y);
    [UIView animateWithDuration:0.05 animations:^{
        if (offSetY > 50 && offSetY <= 120) {
            [scrollView setContentOffset:CGPointMake(0, 120) animated:YES];
        }
        if (offSetY > 0 && offSetY <= 50) {
            [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }
    }];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

{
//    NSLog(@"555555555555555---%.2f",scrollView.contentOffset.y);

}


-(void)initTabView
{
    self.view.userInteractionEnabled = YES;
    self.view.backgroundColor = kGrayColor(241);
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.mas_bottom).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(0);
    }];
    
    // 设置header
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    UIImage *gifImage = [UIImage sf_animatedGIFNamed:@"pull_down_loading"];
    [header setImages:@[gifImage] forState:MJRefreshStateRefreshing];
    [header setImages:@[[UIImage imageNamed:@"line_bg_image"]] forState:MJRefreshStateIdle];
    self.tableView.mj_header = header;

    // 设置footer
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [footer setTitle:LOCAL(@"pullup_loading") forState:MJRefreshStateIdle];
    [footer setTitle:LOCAL(@"loading_more_data") forState:MJRefreshStateRefreshing];
    [footer setTitle:LOCAL(@"no_more") forState:MJRefreshStateNoMoreData];
    self.tableView.mj_footer = footer;
}

- (void)loadNewData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    });
   
}

- (void)loadMoreData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    });
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[SFTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setBackgroundColor:UIMainBackColor];
        _tableView.tableFooterView = [[UIView alloc] init];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.tag = 1000;

    }
    return _tableView;
}


#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"cell--%@--",self.dataArr[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
}


- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < 10; i ++) {
            [_dataArr addObject:@(i)];
        }
    }
    return _dataArr;
}


@end


@interface SFTableView()


@end
@implementation SFTableView
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
@end
@implementation UCButton
- (instancetype )initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:15];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat btnHeight = self.frame.size.height-10;
    self.imageView.center = CGPointMake(self.frame.size.width/2, btnHeight*2/3/2+5);
    self.imageView.bounds = CGRectMake(0, 0, btnHeight*2/3-5, btnHeight*2/3-5);
    self.titleLabel.frame = CGRectMake(0, btnHeight*2/3+5, self.frame.size.width, btnHeight/3);
}
@end

你可能感兴趣的:(iOS自定义TableView收缩动画)