AttendanceCenter——iOS 签到动画库。

21183856_2DSj.gif

GitHub地址:https://github.com/Drmshow/AttendanceCenter
TimeLineView.h

#import <UIKit/UIKit.h>

@interface TimeLineView : UIView

@end

TimeLineView.m

#import "TimeLineView.h"

#define kColorValue(value)  value/255.0

@implementation TimeLineView

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGFloat centerX = self.center.x;
    CGFloat height = self.frame.size.height;
    CGContextMoveToPoint(ctx, centerX, 0.0);
    CGContextAddLineToPoint(ctx, centerX, height);
    [[UIColor colorWithRed:kColorValue(0) green:kColorValue(214) blue:kColorValue(165) alpha:1] setStroke];
    CGContextSetLineWidth(ctx, 4);
    CGContextStrokePath(ctx);
}

@end

AttendanceTableViewCell.h

#import <UIKit/UIKit.h>

@interface AttendanceTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@end

AttendanceTableViewCell.m

#import "AttendanceTableViewCell.h"

#define kColorValue(value)  value/255.0
#define kTimeLineX          22
#define kLastCellTag        11
#define kNodeWidth          15

@interface AttendanceTableViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UIView *bgView;

@end
@implementation AttendanceTableViewCell

- (void)awakeFromNib
{
    self.bgView.layer.cornerRadius = 5;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGFloat height = self.frame.size.height;
    CGContextMoveToPoint(ctx, kTimeLineX, 0.0);
    CGContextAddLineToPoint(ctx, kTimeLineX, height-35);
    [[UIColor colorWithRed:kColorValue(0) green:kColorValue(214) blue:kColorValue(165) alpha:1] setStroke];
    CGContextSetLineWidth(ctx, 4);
    CGContextStrokePath(ctx);
    
    if (self.tag != kLastCellTag) {
        CGContextMoveToPoint(ctx, kTimeLineX, 55 +kNodeWidth *0.5);
        CGContextAddLineToPoint(ctx, kTimeLineX, height);
        [[UIColor colorWithRed:kColorValue(0) green:kColorValue(214) blue:kColorValue(165) alpha:1] setStroke];
        CGContextSetLineWidth(ctx, 4);
        CGContextStrokePath(ctx);
    }
    UIImage *image = [UIImage imageNamed:@"node"];
    [image drawInRect:CGRectMake(kNodeWidth, 55 -kNodeWidth *0.5, kNodeWidth, kNodeWidth)];
}

@end

AttendanceViewController.h

#import <UIKit/UIKit.h>

@interface AttendanceViewController : UIViewController

@end

AttendanceViewController.m

#import "AttendanceViewController.h"
#import "AttendanceTableViewCell.h"
#import "TimeLineView.h"

#define kCellHeight             90
#define kDelayFactor            0.3
#define kAnimationDuration      0.6

@interface AttendanceViewController ()
@property (nonatomic, weak) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *earthImageView;
@property (weak, nonatomic) IBOutlet UIImageView *markerImageView;
@property (nonatomic, assign) BOOL isAnimating;
@property (nonatomic, strong) UIView *coverView;
@property (nonatomic, weak) TimeLineView *timeLineView;
@property (nonatomic, strong) TimeLineView *tableLine;
@property (nonatomic, strong) NSMutableArray *attendanceArrays;
@end

@implementation AttendanceViewController

#pragma mark -View life cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"AttendanceTableViewCell" bundle:nil] forCellReuseIdentifier:@"attendanceTableViewCell"];
}

#pragma mark -UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.attendanceArrays.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AttendanceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"attendanceTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell.tag == 11) {
        cell.tag = 0;
        [cell setNeedsDisplay];
    }
    if (indexPath.row == self.attendanceArrays.count-1) {
        cell.tag = 11;
        [cell setNeedsDisplay];
    }
    return cell;
}

#pragma mark -UITableViewDelegate

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

#pragma mark -Button attendanceCenter click event

- (IBAction)attendanceCenterClick {
    if (self.isAnimating) {
        NSLog(@"正在动画...");
        return;
    }
    NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
    self.coverView.hidden = NO;
    [self.attendanceArrays insertObject:@"1" atIndex:0];
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];

    AttendanceTableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
    UIView *newCell = [cell snapshotViewAfterScreenUpdates:YES];
    newCell.frame = CGRectMake(0, -kCellHeight, cell.frame.size.width, kCellHeight);
    [self.coverView insertSubview:newCell aboveSubview:self.timeLineView];
    [self markerImageViewBeginAnimation];
    [self earthImageViewBeginAnimation];
    [self cellAnimation];
}

#pragma mark -Button attendanceSuccess click event

- (IBAction)attendanceSuccessClick
{
    CALayer *layer = self.markerImageView.layer;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(layer.position.x, layer.position.y-15)];
    animation.autoreverses = YES;
    animation.duration = kAnimationDuration;
    animation.repeatCount = 2;
    animation.removedOnCompletion = NO;
    [layer addAnimation:animation forKey:nil];
}

#pragma mark -Cell animation

- (void)cellAnimation
{
    NSInteger tableViewH = [UIScreen mainScreen].bounds.size.height -100 -64 -100;
    NSInteger cellCount = 2 + tableViewH / kCellHeight;
    self.isAnimating = YES;
    NSInteger index = 0;
    NSInteger count = self.coverView.subviews.count -1;
    if (count > cellCount) {
        count = cellCount;
    }
    self.timeLineView.frame = CGRectMake(1, 0, 40, (count-1) *kCellHeight);
    for (NSInteger i=count; i>=0; i--) {
        if (i == 0) {
            return;
        }
        UIView *subview = self.coverView.subviews[i];
        if ([subview isKindOfClass:[TimeLineView class]]) {
            continue;
        }
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(subview.layer.position.x, subview.layer.position.y+kCellHeight)];
        animation.duration = kAnimationDuration -0.1;
        animation.removedOnCompletion = NO;
        if (i == 1) {
            animation.delegate = self;
        }
        animation.fillMode = kCAFillModeForwards;
        animation.beginTime = CACurrentMediaTime()+kDelayFactor*index +kAnimationDuration;
        [subview.layer addAnimation:animation forKey:nil];
        index++;
    }
}

#pragma mark -CAAnimationDelegate

- (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag
{
    self.coverView.hidden = YES;
    self.isAnimating = NO;
    for (UIView *subview in self.coverView.subviews) {
        if ([subview isKindOfClass:[TimeLineView class]]) {
            continue;
        }
        [subview.layer removeAllAnimations];
        subview.frame = CGRectOffset(subview.frame, 0, kCellHeight);
    }
}

#pragma mark -MarkerImageView animation

- (void)markerImageViewBeginAnimation
{
    CALayer *layer = self.markerImageView.layer;
    CABasicAnimation *animTranslate = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    animTranslate.toValue = @(-46-15);
    CABasicAnimation *animScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animScale.toValue = @(1.5);
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[animTranslate];
    group.duration = kAnimationDuration;
    group.autoreverses = YES;
    [layer addAnimation:group forKey:nil];
}

#pragma mark -EarthImageView animation

- (void)earthImageViewBeginAnimation
{
    CALayer *layer = self.earthImageView.layer;
    CABasicAnimation *animTranslate = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    animTranslate.toValue = @(-30);
    CABasicAnimation *animScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animScale.toValue = @(1.2);
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[animTranslate, animScale];
    group.duration = kAnimationDuration;
    group.autoreverses = YES;
    [layer addAnimation:group forKey:nil];
}

#pragma mark -lazy coverView

- (UIView *)coverView
{
    if (!_coverView) {
        _coverView = [[UIView alloc] initWithFrame:self.tableView.frame];
        _coverView.backgroundColor = [UIColor whiteColor];
        self.tableView.backgroundColor = [UIColor whiteColor];
        TimeLineView *timeLineView = [[TimeLineView alloc] initWithFrame:CGRectMake(1, 0, 40, _coverView.frame.size.height -35)];
        self.timeLineView = timeLineView;
        timeLineView.backgroundColor = [UIColor whiteColor];
        [_coverView addSubview:timeLineView];
        NSArray *visibleCells = [self.tableView visibleCells];
        for (AttendanceTableViewCell *cell in visibleCells) {
            UIView *copyCell = [cell snapshotViewAfterScreenUpdates:YES];
            copyCell.frame = cell.frame;
            [_coverView addSubview:copyCell];
        }
        
    }
    [self.view insertSubview:_coverView belowSubview:self.bottomView];
    [self.view bringSubviewToFront:self.headerView];
    return _coverView;
}

#pragma mark -lazy attendanceArrays

- (NSMutableArray *)attendanceArrays
{
    if (!_attendanceArrays) {
        _attendanceArrays = [NSMutableArray array];
        [_attendanceArrays addObject:@"1"];
        [_attendanceArrays addObject:@"1"];
    }
    return _attendanceArrays;
}

#pragma mark -TableView timeLine

- (TimeLineView *)tableLine
{
    if (!_tableLine) {
        _tableLine = [[TimeLineView alloc] init];
        _tableLine.backgroundColor = self.tableView.backgroundColor;
        [self.tableView addSubview:_tableLine];
    }
    return _tableLine;
}

#pragma  mark -UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y < 0) {
        self.tableLine.frame = CGRectMake(1, scrollView.contentOffset.y, 40, -scrollView.contentOffset.y);
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.isAnimating) {
        return;
    }
    [self markerImageViewBeginAnimation];
    [self earthImageViewBeginAnimation];
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,396评论 2 45
  • 彼岸 文|默默 一 从此岸到彼岸,一条 汹涌的河,一个 遥远的梦 岁月,是一条流淌的河 你在我的,我在你的 彼岸
    荔园默默阅读 259评论 7 16
  • 今天好冷,空调还是平时的温度,办公室的同事们却都是一幅畏首畏尾的神态,搓着手,缩着脖子。似乎都不在工作状态,可是又...
    华采衣阅读 251评论 0 2