iOS 点击段头实现伸缩动画

2017-10-12  本文已影响227人  sunnnybear

原文Github地址:YouXianMing

gif效果

这个动画比较简单,主要思路为以下几个步骤:

1.自定义TableView段头并设置点击事件

2.点击后判断是否展开,如果将要展开就插入cell;如果将要闭合就删除cell

3.最后实现一些动画效果

以下是自定义UITableViewHeaderFooterView代码
.h文件

#import <UIKit/UIKit.h>
#import "HeaderModel.h"

@protocol RootHeaderFootViewDelegate <NSObject>

- (void)customHeaderFooterView;

@end

@interface RootHeaderFooterView : UITableViewHeaderFooterView
/** model */
@property (nonatomic, strong)HeaderModel *model;

@property (nonatomic, weak)UITableView *tableView;
@property (nonatomic, weak)UIViewController *viewController;
@property (nonatomic, weak)id <RootHeaderFootViewDelegate>delegate;
@property (nonatomic,assign) NSInteger section;
-(void)loadContent;
@end

.m文件

#import "RootHeaderFooterView.h"

@interface RootHeaderFooterView()
/**  */
@property (nonatomic, strong)UILabel *normalNameLabel;
/**  */
@property (nonatomic, strong)UILabel *highNameLabel;
/**  */
@property (nonatomic, strong)UIView *rotateView;

@end

@implementation RootHeaderFooterView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor orangeColor];
        self.model = [[HeaderModel alloc] init];
        self.userInteractionEnabled = YES;
        [self setupView];
    }
    return self;
}

- (void)setupView{
    self.normalNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 26)];
    self.normalNameLabel.font = [UIFont fontWithName:@"AppleSDGothicNeo" size:12];
    [self.contentView addSubview:self.normalNameLabel];
    
    self.highNameLabel = [[UILabel alloc] initWithFrame:self.normalNameLabel.frame];
    self.highNameLabel.font = self.normalNameLabel.font;
    self.highNameLabel.textColor = [UIColor redColor];
    [self.contentView addSubview:self.highNameLabel];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 400, 30)];
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn];
}

- (void)loadContent {
    self.normalNameLabel.text = self.model.className;
    self.highNameLabel.text = self.model.className;
    
    if (self.model.isOpen) {
        [self openTableAnimated];
    }else{
        [self closeTableAnimated];
    }
}

- (void)closeTableAnimated{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.normalNameLabel.alpha = 1;
        self.normalNameLabel.frame = CGRectMake(10 , 0, 100, 26);
        
        self.highNameLabel.alpha = 0;
        self.highNameLabel.frame = CGRectMake(10 , 0, 100, 26);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)openTableAnimated{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.normalNameLabel.alpha = 0;
        self.normalNameLabel.frame = CGRectMake(10 + 10, 0, 100, 26);
        
        self.highNameLabel.alpha = 1;
        self.highNameLabel.frame = CGRectMake(10 + 10, 0, 100, 26);
        self.contentView.backgroundColor = [UIColor whiteColor];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.contentView.backgroundColor = [UIColor orangeColor];
        } completion:nil];
        
    }];
}

- (void)clicked:(UIButton *)btn{
    
    if (self.model.isOpen) {
        self.model.isOpen = false;
        [self closeTableAnimated];
        NSMutableArray<NSIndexPath*> *indexPaths = [[NSMutableArray alloc] init];
        
        NSInteger index = self.model.students.count;
        for (NSInteger i = 0; i<index; i++) {
            NSIndexPath * indexpath = [NSIndexPath indexPathForItem:i inSection:self.section];
            [indexPaths addObject:indexpath];
            
        }
        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }else {
        self.model.isOpen = true;
        [self openTableAnimated];
        NSMutableArray<NSIndexPath*> *indexPaths = [[NSMutableArray alloc] init];
        
        NSInteger index = self.model.students.count;
        for (NSInteger i = 0; i<index; i++) {
            NSIndexPath * indexpath = [NSIndexPath indexPathForItem:i inSection:self.section];
            [indexPaths addObject:indexpath];
            
        }
        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }
}
@end

上一篇下一篇

猜你喜欢

热点阅读