No Description

FKSignDoneView.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // FKSignDoneView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/3/14.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKSignDoneView.h"
  9. #define kBgImgViewBottomMargin ((IS_IPHONE_6P || IS_IPHONE_X) ? 200 : 120)
  10. #define kSignAnimDuration 0.3f
  11. @interface FKSignDoneView ()
  12. @property (nonatomic, strong) UIView *contentView;
  13. @property (nonatomic, strong) UIImageView *bgImgView;
  14. @property (nonatomic, strong) UILabel *addPointLabel;
  15. @property (nonatomic, strong) UILabel *tomorrowLabel;
  16. @property (nonatomic, strong) NSLayoutConstraint *bgViewConstraint;
  17. @end
  18. @implementation FKSignDoneView
  19. - (instancetype)initWithNextDay:(NSString *)nextDay score:(NSString *)score{
  20. if (self = [super init]) {
  21. [self addAllSubviews];
  22. self.addPointLabel.attributedText = [self attStrWithScore:score];
  23. self.tomorrowLabel.text = nextDay;
  24. }
  25. return self;
  26. }
  27. - (void)addAllSubviews{
  28. [self addSubview:self.contentView];
  29. [self.contentView addSubview:self.bgImgView];
  30. [self.bgImgView addSubview:self.addPointLabel];
  31. [self.bgImgView addSubview:self.tomorrowLabel];
  32. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.edges.insets(UIEdgeInsetsZero);
  34. }];
  35. [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.bottom.equalTo(self.contentView).offset(200);
  37. make.centerX.equalTo(self.contentView);
  38. }];
  39. [self.addPointLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.top.equalTo(self.bgImgView).offset(45);
  41. make.centerX.equalTo(self.bgImgView);
  42. }];
  43. [self.tomorrowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.bottom.equalTo(self.bgImgView).offset(- 70);
  45. make.centerX.equalTo(self.bgImgView);
  46. }];
  47. }
  48. + (void)showInView:(UIView *)view
  49. withNextDay:(NSString *)nextDay
  50. score:(NSString *)score
  51. bgImage:(UIImage *)bgImage
  52. completion:(void (^)(BOOL finished))completion {
  53. if (!view) return;
  54. for (FKSignDoneView *doneView in view.subviews) {
  55. if ([doneView isKindOfClass:[FKSignDoneView class]]){
  56. [doneView removeFromSuperview];
  57. }
  58. }
  59. FKSignDoneView *doneView = [[FKSignDoneView alloc]initWithNextDay:nextDay score:score];
  60. if (bgImage) {
  61. doneView.bgImgView.image = bgImage;
  62. }
  63. [view addSubview:doneView];
  64. for (UIView *subview in view.subviews) {
  65. NSLog(@"subview = %@", subview);
  66. }
  67. [doneView mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.edges.insets(UIEdgeInsetsZero);
  69. }];
  70. [doneView setNeedsLayout];
  71. [doneView layoutIfNeeded];
  72. __weak typeof(doneView) block_doneView = doneView;
  73. [UIView animateWithDuration:kSignAnimDuration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
  74. block_doneView.contentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  75. block_doneView.bgViewConstraint.constant = - kBgImgViewBottomMargin;
  76. [block_doneView setNeedsLayout];
  77. [block_doneView layoutIfNeeded];
  78. } completion:^(BOOL finished) {
  79. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  80. [UIView animateWithDuration:kSignAnimDuration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
  81. block_doneView.contentView.backgroundColor = [UIColor clearColor];
  82. block_doneView.bgViewConstraint.constant = 200;
  83. [block_doneView setNeedsLayout];
  84. [block_doneView layoutIfNeeded];
  85. } completion:^(BOOL finished) {
  86. [block_doneView removeFromSuperview];
  87. if (completion) {
  88. completion(finished);
  89. }
  90. }];
  91. });
  92. }];
  93. }
  94. - (NSAttributedString *)attStrWithScore:(NSString *)score{
  95. if (!score.length) return nil;
  96. NSString *fullStr = [score stringByAppendingString:@" 积分"];
  97. NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:fullStr];
  98. [att addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:30] range:NSMakeRange(0, score.length)];
  99. [att addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x333333) range:NSMakeRange(0, score.length)];
  100. return att;
  101. }
  102. #pragma mark - property
  103. - (UIView *)contentView{
  104. if (_contentView == nil) {
  105. _contentView = [[UIView alloc]init];
  106. _contentView.backgroundColor = [UIColor clearColor];
  107. }
  108. return _contentView;
  109. }
  110. - (UIImageView *)bgImgView{
  111. if (_bgImgView == nil) {
  112. _bgImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sign_bg"]];
  113. }
  114. return _bgImgView;
  115. }
  116. - (UILabel *)addPointLabel{
  117. if (_addPointLabel == nil) {
  118. _addPointLabel = [[UILabel alloc]init];
  119. _addPointLabel.font = [UIFont systemFontOfSize:14];
  120. _addPointLabel.textColor = UIColorFromRGB(0x999999);
  121. }
  122. return _addPointLabel;
  123. }
  124. - (UILabel *)tomorrowLabel{
  125. if (_tomorrowLabel == nil) {
  126. _tomorrowLabel = [[UILabel alloc]init];
  127. _tomorrowLabel.font = [UIFont systemFontOfSize:14];
  128. _tomorrowLabel.textColor = UIColorFromRGB(0x666666);
  129. // _tomorrowLabel.text = @"明日签到可以获得30积分";
  130. }
  131. return _tomorrowLabel;
  132. }
  133. - (NSLayoutConstraint *)bgViewConstraint{
  134. if (_bgViewConstraint == nil) {
  135. for (NSLayoutConstraint *constraint in self.contentView.constraints) {
  136. if (constraint.firstItem == self.bgImgView && constraint.firstAttribute == NSLayoutAttributeBottom){
  137. _bgViewConstraint = constraint;
  138. break;
  139. }
  140. }
  141. }
  142. return _bgViewConstraint;
  143. }
  144. @end