// // FLRiseViewHelper.m // FirstLink // // Created by unicode on 14-11-18. // Copyright (c) 2014年 FirstLink. All rights reserved. // #import "FloatDownView.h" #import "FirstLinkAppDelegate.h" #import "FLControllerHelper.h" @interface FloatDownView () @property (nonatomic, assign) BOOL isShowing; @property (nonatomic, strong) UILabel *textLabel; @property (nonatomic, strong) UIImageView *leftIconImageView; @property (nonatomic, strong) UIButton *rightCancelBtn; @property (nonatomic, strong) UITapGestureRecognizer *clickMoreGestureRecognizer; @property (nonatomic, strong) UITapGestureRecognizer *clickCloseGestureRecognizer; @property (nonatomic, weak) UIView *pView; @property (nonatomic, assign) CGRect raiseViewFrame; @property (nonatomic, copy) void (^callBack)(id object); @end @implementation FloatDownCommand @end @implementation FloatDownView + (FloatDownView *)sharedInstance { static FloatDownView *sharedRiseViewHelperInstance = nil; static dispatch_once_t once_token; dispatch_once(&once_token, ^{ sharedRiseViewHelperInstance = [[self alloc] init]; }); return sharedRiseViewHelperInstance; } - (void)prepareFloatDownViewFrame:(CGRect)frame inView:(UIView *)pView clickCallBack:(void (^)(id))callBack { self.raiseViewFrame = frame; self.pView = pView; self.callBack = callBack; } - (void)sendCommand:(FloatDownCommand *)command { if (self.messageQueue.count) return; // 一次只显示一个 if (command) { [self.messageQueue addObject:command]; } FloatDownCommand *latestCommand = self.messageQueue.lastObject; if (latestCommand) { switch (latestCommand.type) { case RiseViewTypeDefault: { break; } case RiseViewTypeAutoClose: { [self layoutForAutoCloseView]; [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(closeView:) userInfo:nil repeats:FALSE]; break; } case RiseViewTypeClickClose: { [self layoutForAutoClickCloseView]; // [self.textLabel addGestureRecognizer:self.clickCloseGestureRecognizer]; break; } case RiseViewTypeClickMore: { [self.textLabel addGestureRecognizer:self.clickMoreGestureRecognizer]; break; } default: break; } [self riseBannerInView:self.pView content:latestCommand.content bgcolor:[self bgcolorWithType:latestCommand.type] textColor:[self textColorWithType:latestCommand.type] type:latestCommand.type]; } } - (UIColor*)bgcolorWithType:(RiseViewType)type{ switch (type) { case RiseViewTypeDefault: case RiseViewTypeAutoClose: case RiseViewTypeClickClose: { return [UIColor colorWithRed:1.0 green:99.0/255.0 blue:99.0/255.0 alpha:0.9]; break; } case RiseViewTypeClickMore: { return [UIColor yellowColor]; break; } default: break; } return [UIColor blackColor]; } - (UIColor*)textColorWithType:(RiseViewType)type{ switch (type) { case RiseViewTypeDefault: case RiseViewTypeAutoClose: case RiseViewTypeClickClose: case RiseViewTypeClickMore: { return [UIColor whiteColor]; break; } default: break; } return [UIColor whiteColor]; } - (void)riseBannerInView:(UIView *)pView content:(NSString *)content bgcolor:(UIColor *)bgcolor textColor:(UIColor *)textColor type:(RiseViewType)type { self.textLabel.textColor = textColor; self.textLabel.text = content; self.backgroundColor = bgcolor; self.frame = CGRectOffset(self.raiseViewFrame, 0, - DOWN_COMMAND_HEIGHT); [pView addSubview:self]; __weak FloatDownView *weakSelf = self; [UIView animateWithDuration:0.4 animations:^{ weakSelf.frame = weakSelf.raiseViewFrame; } completion:^(BOOL finished) { if (type == RiseViewTypeClickClose) { // [weakSelf addSubview:weakSelf.closeImageView]; } }]; } #pragma mark - - (void)layoutForAutoCloseView { [self clearContent]; self.backgroundColor = [UIColor colorWithRed:73.0/255.0 green:208/255.0 blue:187.0/255.0 alpha:0.9]; WeakSelf(weakSelf); [self addSubview:self.textLabel]; [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.equalTo(weakSelf).with.offset(0); make.centerX.equalTo(weakSelf).with.offset(12); make.width.mas_greaterThanOrEqualTo(10); }]; [self addSubview:self.leftIconImageView]; [self.leftIconImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(weakSelf.textLabel.mas_left).with.offset(-12); make.centerY.equalTo(weakSelf); make.size.mas_equalTo(CGSizeMake(15, 15)); }]; } - (void)layoutForAutoClickCloseView{ [self clearContent]; self.backgroundColor = [UIColorFromRGB(0xff6362) colorWithAlphaComponent:0.9]; [self addSubview:self.textLabel]; [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsZero); }]; [self addSubview:self.rightCancelBtn]; [self.rightCancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self); make.width.mas_equalTo(45); make.height.centerY.equalTo(self); }]; } - (void)clearContent{ for (UIView *subView in self.subviews) { [subView removeFromSuperview]; } } #pragma mark - Generate - (NSMutableArray*)messageQueue { if (!_messageQueue) { _messageQueue = [NSMutableArray array]; } return _messageQueue; } - (UILabel*)textLabel { if (!_textLabel) { _textLabel = [[UILabel alloc] init]; _textLabel.textAlignment = NSTextAlignmentCenter; _textLabel.font = [UIFont systemFontOfSize:13.0]; } return _textLabel; } - (UIImageView*)leftIconImageView { if (!_leftIconImageView) { _leftIconImageView = [[UIImageView alloc] init]; _leftIconImageView.image = [UIImage imageNamed:@"NewContentImageIcon"]; } return _leftIconImageView; } - (UIButton *)rightCancelBtn{ if (_rightCancelBtn == nil) { _rightCancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_rightCancelBtn setImage:[UIImage imageNamed:@"red_cancel"] forState:UIControlStateNormal]; [_rightCancelBtn addTarget:self action:@selector(clickCancelBtn:) forControlEvents:UIControlEventTouchUpInside]; } return _rightCancelBtn; } - (UITapGestureRecognizer*)clickMoreGestureRecognizer { if (!_clickMoreGestureRecognizer) { _clickMoreGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickMoreAction:)]; _clickMoreGestureRecognizer.numberOfTapsRequired = 1; } return _clickMoreGestureRecognizer; } - (UITapGestureRecognizer*)clickCloseGestureRecognizer { if (!_clickCloseGestureRecognizer) { _clickCloseGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCloseAction:)]; _clickCloseGestureRecognizer.numberOfTapsRequired = 1; } return _clickCloseGestureRecognizer; } #pragma mark - Action Method - (IBAction)closeView:(id)sender { __weak FloatDownView *weakSelf = self; [UIView animateWithDuration:0.4 animations:^{ weakSelf.frame = CGRectMake(0, -CGRectGetHeight(weakSelf.raiseViewFrame), [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(weakSelf.raiseViewFrame)); } completion:^(BOOL finished) { [weakSelf removeFromSuperview]; [weakSelf.messageQueue removeAllObjects]; }]; } - (IBAction)clickMoreAction:(id)sender { [self closeView:nil]; if (self.callBack) { self.callBack(nil); } } - (void)clickCancelBtn:(UIButton *)sender{ [self closeView:nil]; } - (IBAction)clickCloseAction:(id)sender { [self closeView:nil]; } @end