// // FKSignDoneView.m // FirstLink // // Created by jack on 16/3/14. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKSignDoneView.h" #define kBgImgViewBottomMargin ((IS_IPHONE_6P || IS_IPHONE_X) ? 200 : 120) #define kSignAnimDuration 0.3f @interface FKSignDoneView () @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UIImageView *bgImgView; @property (nonatomic, strong) UILabel *addPointLabel; @property (nonatomic, strong) UILabel *tomorrowLabel; @property (nonatomic, strong) NSLayoutConstraint *bgViewConstraint; @end @implementation FKSignDoneView - (instancetype)initWithNextDay:(NSString *)nextDay score:(NSString *)score{ if (self = [super init]) { [self addAllSubviews]; self.addPointLabel.attributedText = [self attStrWithScore:score]; self.tomorrowLabel.text = nextDay; } return self; } - (void)addAllSubviews{ [self addSubview:self.contentView]; [self.contentView addSubview:self.bgImgView]; [self.bgImgView addSubview:self.addPointLabel]; [self.bgImgView addSubview:self.tomorrowLabel]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsZero); }]; [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.contentView).offset(200); make.centerX.equalTo(self.contentView); }]; [self.addPointLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.bgImgView).offset(45); make.centerX.equalTo(self.bgImgView); }]; [self.tomorrowLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.bgImgView).offset(- 70); make.centerX.equalTo(self.bgImgView); }]; } + (void)showInView:(UIView *)view withNextDay:(NSString *)nextDay score:(NSString *)score bgImage:(UIImage *)bgImage completion:(void (^)(BOOL finished))completion { if (!view) return; for (FKSignDoneView *doneView in view.subviews) { if ([doneView isKindOfClass:[FKSignDoneView class]]){ [doneView removeFromSuperview]; } } FKSignDoneView *doneView = [[FKSignDoneView alloc]initWithNextDay:nextDay score:score]; if (bgImage) { doneView.bgImgView.image = bgImage; } [view addSubview:doneView]; for (UIView *subview in view.subviews) { NSLog(@"subview = %@", subview); } [doneView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsZero); }]; [doneView setNeedsLayout]; [doneView layoutIfNeeded]; __weak typeof(doneView) block_doneView = doneView; [UIView animateWithDuration:kSignAnimDuration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{ block_doneView.contentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; block_doneView.bgViewConstraint.constant = - kBgImgViewBottomMargin; [block_doneView setNeedsLayout]; [block_doneView layoutIfNeeded]; } completion:^(BOOL finished) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:kSignAnimDuration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{ block_doneView.contentView.backgroundColor = [UIColor clearColor]; block_doneView.bgViewConstraint.constant = 200; [block_doneView setNeedsLayout]; [block_doneView layoutIfNeeded]; } completion:^(BOOL finished) { [block_doneView removeFromSuperview]; if (completion) { completion(finished); } }]; }); }]; } - (NSAttributedString *)attStrWithScore:(NSString *)score{ if (!score.length) return nil; NSString *fullStr = [score stringByAppendingString:@" 积分"]; NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:fullStr]; [att addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:30] range:NSMakeRange(0, score.length)]; [att addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x333333) range:NSMakeRange(0, score.length)]; return att; } #pragma mark - property - (UIView *)contentView{ if (_contentView == nil) { _contentView = [[UIView alloc]init]; _contentView.backgroundColor = [UIColor clearColor]; } return _contentView; } - (UIImageView *)bgImgView{ if (_bgImgView == nil) { _bgImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sign_bg"]]; } return _bgImgView; } - (UILabel *)addPointLabel{ if (_addPointLabel == nil) { _addPointLabel = [[UILabel alloc]init]; _addPointLabel.font = [UIFont systemFontOfSize:14]; _addPointLabel.textColor = UIColorFromRGB(0x999999); } return _addPointLabel; } - (UILabel *)tomorrowLabel{ if (_tomorrowLabel == nil) { _tomorrowLabel = [[UILabel alloc]init]; _tomorrowLabel.font = [UIFont systemFontOfSize:14]; _tomorrowLabel.textColor = UIColorFromRGB(0x666666); // _tomorrowLabel.text = @"明日签到可以获得30积分"; } return _tomorrowLabel; } - (NSLayoutConstraint *)bgViewConstraint{ if (_bgViewConstraint == nil) { for (NSLayoutConstraint *constraint in self.contentView.constraints) { if (constraint.firstItem == self.bgImgView && constraint.firstAttribute == NSLayoutAttributeBottom){ _bgViewConstraint = constraint; break; } } } return _bgViewConstraint; } @end