123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //
- // LaunchAnimation.m
- // FirstLink
- //
- // Created by ascii on 15/6/27.
- // Copyright (c) 2015年 FirstLink. All rights reserved.
- //
- #import "FKLaunchAnimation.h"
- #import "UserDefaultManager.h"
- @interface FKLaunchAnimation ()
- @property (nonatomic, strong) NSTimer *timer;
- @property (nonatomic, assign) NSInteger timerCount;
- @property (nonatomic, strong) UIImageView *bottomImgView;
- @property (nonatomic, strong) UIButton *skipButton;
- @property (nonatomic, copy) void (^complete)(BOOL, NSString *);
- @end
- static BOOL gLaunchAnimationFinished;
- @implementation FKLaunchAnimation
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.timerCount = 3;
- gLaunchAnimationFinished = NO;
-
- [self addAllSubviews];
- }
- return self;
- }
- - (instancetype)init {
- return [super initWithFrame:CGRectZero];
- }
- #pragma mark - Static
- + (BOOL)isFinished {
- return gLaunchAnimationFinished;
- }
- #pragma mark - API
- - (void)startAnimation:(void (^)(BOOL, NSString *))complete {
- self.complete = complete;
-
- UIImage *launchImage = [FKLaunchStore launchImage];
- if (launchImage) {
- self.bottomImgView.image = launchImage;
- NSString *targetURL = [[UserDefaultManager sharedManager] getUserDefaultObject:LAUNCH_TARGET_URL_KEY];
- if ([targetURL isKindOfClass:[NSString class]] && targetURL.length > 0) {
- self.skipButton.hidden = NO;
- [self.skipButton setTitle:[NSString stringWithFormat:@"跳过%@", @(self.timerCount)] forState:UIControlStateNormal];
- self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerDecreaseCallBack) userInfo:nil repeats:YES];
- } else {
- self.skipButton.hidden = YES;
- self.timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(finishAnimation:) userInfo:nil repeats:NO];
- }
- } else {
- [self finishAnimation:nil];
- }
- }
- #pragma mark - Action
- - (void)timerDecreaseCallBack {
- _timerCount--;
- if (self.timerCount > 0) {
- // show time decrease
- [self.skipButton setTitle:[NSString stringWithFormat:@"跳过%ld", (long)self.timerCount] forState:UIControlStateNormal];
- } else {
- [self finishAnimation:nil];
- }
- }
- - (IBAction)clickBottomImgView:(id)sender {
- NSString *targetURL = [[UserDefaultManager sharedManager] getUserDefaultObject:LAUNCH_TARGET_URL_KEY];
- [self finishAnimation:targetURL];
- }
- - (IBAction)clickSkipButton:(id)sender {
- [self finishAnimation:nil];
- }
- #pragma mark - Method
- - (void)finishAnimation:(NSString *)targetURL {
- [self.timer invalidate];
- self.timer = nil;
-
- gLaunchAnimationFinished = YES;
-
- if (self.complete) {
- self.complete(YES, targetURL);
- }
- }
- #pragma mark - Layout
- - (void)addAllSubviews {
- [self addSubview:self.bottomImgView];
- [self.bottomImgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.bottom.equalTo(self);
- }];
-
- [self addSubview:self.skipButton];
- [self.skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self).offset(-15);
- make.top.equalTo(self).offset(IS_IPHONE_X ? 44 : 30);
- make.size.mas_equalTo(CGSizeMake(70, 24));
- }];
- }
- #pragma mark - Property
- - (UIImageView*)bottomImgView {
- if (!_bottomImgView) {
- _bottomImgView = [[UIImageView alloc] init];
- _bottomImgView.backgroundColor = [UIColor whiteColor];
- _bottomImgView.userInteractionEnabled = YES;
- [_bottomImgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBottomImgView:)]];
- }
- return _bottomImgView;
- }
- - (UIButton *)skipButton {
- if (!_skipButton) {
- _skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
- _skipButton.hidden = YES;
- _skipButton.layer.cornerRadius = 12;
- _skipButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
- [_skipButton setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
- [_skipButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
- [_skipButton addTarget:self action:@selector(clickSkipButton:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _skipButton;
- }
- @end
|