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閱讀 228,923評論 6 535
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,740評論 3 420
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,856評論 0 380
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,175評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,931評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,321評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,383評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,533評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,082評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,891評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,067評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,618評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,319評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,732評論 0 27
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,987評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,794評論 3 394
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,076評論 2 375

推薦閱讀更多精彩內容

  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,468評論 2 45
  • 彼岸 文|默默 一 從此岸到彼岸,一條 洶涌的河,一個 遙遠的夢 歲月,是一條流淌的河 你在我的,我在你的 彼岸
    荔園默默閱讀 271評論 7 16
  • 今天好冷,空調還是平時的溫度,辦公室的同事們卻都是一幅畏首畏尾的神態,搓著手,縮著脖子。似乎都不在工作狀態,可是又...
    華采衣閱讀 254評論 0 2