Няма описание

FKLaunchAnimation.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // LaunchAnimation.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 15/6/27.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKLaunchAnimation.h"
  9. #import "UserDefaultManager.h"
  10. @interface FKLaunchAnimation ()
  11. @property (nonatomic, strong) NSTimer *timer;
  12. @property (nonatomic, assign) NSInteger timerCount;
  13. @property (nonatomic, strong) UIImageView *bottomImgView;
  14. @property (nonatomic, strong) UIButton *skipButton;
  15. @property (nonatomic, copy) void (^complete)(BOOL, NSString *);
  16. @end
  17. static BOOL gLaunchAnimationFinished;
  18. @implementation FKLaunchAnimation
  19. /*
  20. // Only override drawRect: if you perform custom drawing.
  21. // An empty implementation adversely affects performance during animation.
  22. - (void)drawRect:(CGRect)rect {
  23. // Drawing code
  24. }
  25. */
  26. - (instancetype)initWithFrame:(CGRect)frame {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. self.timerCount = 3;
  30. gLaunchAnimationFinished = NO;
  31. [self addAllSubviews];
  32. }
  33. return self;
  34. }
  35. - (instancetype)init {
  36. return [super initWithFrame:CGRectZero];
  37. }
  38. #pragma mark - Static
  39. + (BOOL)isFinished {
  40. return gLaunchAnimationFinished;
  41. }
  42. #pragma mark - API
  43. - (void)startAnimation:(void (^)(BOOL, NSString *))complete {
  44. self.complete = complete;
  45. UIImage *launchImage = [FKLaunchStore launchImage];
  46. if (launchImage) {
  47. self.bottomImgView.image = launchImage;
  48. NSString *targetURL = [[UserDefaultManager sharedManager] getUserDefaultObject:LAUNCH_TARGET_URL_KEY];
  49. if ([targetURL isKindOfClass:[NSString class]] && targetURL.length > 0) {
  50. self.skipButton.hidden = NO;
  51. [self.skipButton setTitle:[NSString stringWithFormat:@"跳过%@", @(self.timerCount)] forState:UIControlStateNormal];
  52. self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerDecreaseCallBack) userInfo:nil repeats:YES];
  53. } else {
  54. self.skipButton.hidden = YES;
  55. self.timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(finishAnimation:) userInfo:nil repeats:NO];
  56. }
  57. } else {
  58. [self finishAnimation:nil];
  59. }
  60. }
  61. #pragma mark - Action
  62. - (void)timerDecreaseCallBack {
  63. _timerCount--;
  64. if (self.timerCount > 0) {
  65. // show time decrease
  66. [self.skipButton setTitle:[NSString stringWithFormat:@"跳过%ld", (long)self.timerCount] forState:UIControlStateNormal];
  67. } else {
  68. [self finishAnimation:nil];
  69. }
  70. }
  71. - (IBAction)clickBottomImgView:(id)sender {
  72. NSString *targetURL = [[UserDefaultManager sharedManager] getUserDefaultObject:LAUNCH_TARGET_URL_KEY];
  73. [self finishAnimation:targetURL];
  74. }
  75. - (IBAction)clickSkipButton:(id)sender {
  76. [self finishAnimation:nil];
  77. }
  78. #pragma mark - Method
  79. - (void)finishAnimation:(NSString *)targetURL {
  80. [self.timer invalidate];
  81. self.timer = nil;
  82. gLaunchAnimationFinished = YES;
  83. if (self.complete) {
  84. self.complete(YES, targetURL);
  85. }
  86. }
  87. #pragma mark - Layout
  88. - (void)addAllSubviews {
  89. [self addSubview:self.bottomImgView];
  90. [self.bottomImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.left.top.right.bottom.equalTo(self);
  92. }];
  93. [self addSubview:self.skipButton];
  94. [self.skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.right.equalTo(self).offset(-15);
  96. make.top.equalTo(self).offset(IS_IPHONE_X ? 44 : 30);
  97. make.size.mas_equalTo(CGSizeMake(70, 24));
  98. }];
  99. }
  100. #pragma mark - Property
  101. - (UIImageView*)bottomImgView {
  102. if (!_bottomImgView) {
  103. _bottomImgView = [[UIImageView alloc] init];
  104. _bottomImgView.backgroundColor = [UIColor whiteColor];
  105. _bottomImgView.userInteractionEnabled = YES;
  106. [_bottomImgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBottomImgView:)]];
  107. }
  108. return _bottomImgView;
  109. }
  110. - (UIButton *)skipButton {
  111. if (!_skipButton) {
  112. _skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
  113. _skipButton.hidden = YES;
  114. _skipButton.layer.cornerRadius = 12;
  115. _skipButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
  116. [_skipButton setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
  117. [_skipButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
  118. [_skipButton addTarget:self action:@selector(clickSkipButton:) forControlEvents:UIControlEventTouchUpInside];
  119. }
  120. return _skipButton;
  121. }
  122. @end