YH_BarrageViewController.m 8.88 KB
//
//  YH_Barrage.m
//  YHBarrage
//
//  Created by Arthur on 16/6/6.
//  Copyright © 2016年 YOHO!. All rights reserved.
//

#import "YH_BarrageViewController.h"
#import "YH_BarrageTableView.h"
#import "UtilsMacros.h"
#import "UIView+YOHO.h"
#import "UIFont+LIVE.h"
#import "UIColor+YOHO.h"

#define kBarrageTableViewHeight 253
#define kBarrageTableViewWidth  (245 * kScreenPointScale)
#define kInputTextFieldHeight 34
#define kInputTextFieldBottomEdge 49
#define kPaddingLeft 13
#define kCloseButtonWidth 19

#define kMessageTipLeftEdge 111
#define kMessageTipHeight 21.5
#define kMessageTipWidth 87.5

@interface YH_BarrageViewController ()<YH_BarrageTableViewScrollingDelegate, HPGrowingTextViewDelegate>

@property (strong, nonatomic) UIImageView *inputTextBgView;
@property (strong, nonatomic) UIButton *keyBoardCloseButton;
@property (strong, nonatomic) UIView *closeButtonContainer;
@property (strong, nonatomic) UIView *bottomContainerView;
@property (strong, nonatomic) UIImageView *messageTip;
@property (strong, nonatomic) UIImageView *messageArrow;
@property (strong, nonatomic)YH_BarrageTableView *barrageTableView;
@property (nonatomic) YHBarrageType barrageType;
@property (assign, nonatomic) CGSize keyboardSize;
@property (strong, nonatomic) UILabel *newsBubble;
@end

@implementation YH_BarrageViewController

#pragma mark - life cycle

- (instancetype)initWithType:(YHBarrageType)barrageType
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.barrageType = barrageType;
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.barrageTableView];
    [self setBottomView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.barrageType == YHBarrageTypeSend) {
        // 显示输入框
        self.inputTextField.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
        self.inputTextField.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
        self.inputTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    } else if(self.barrageType == YHBarrageTypeReadonly) {
        // 不显示输入框
        self.bottomContainerView.hidden = YES;
        self.messageTip.hidden = YES;
    }
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)setBottomView
{
    CGFloat scalePoint = kScreenHeight / 667.0;

    [self.barrageTableView setFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)-kBarrageTableViewHeight * scalePoint-kInputTextFieldBottomEdge, kBarrageTableViewWidth, kBarrageTableViewHeight * scalePoint)];
    
    self.bottomContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.barrageTableView.frame) + 5, kScreenWidth, kInputTextFieldHeight)];
    self.bottomContainerView.backgroundColor = [UIColor clearColor];
    [self.inputTextField setFrame:CGRectMake(kPaddingLeft, 0, CGRectGetWidth(self.view.bounds) - 2*kPaddingLeft, kInputTextFieldHeight)];
    
    self.inputTextBgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) - 2*kPaddingLeft, kInputTextFieldHeight)];
    self.inputTextBgView.image = [UIImage imageNamed:@"inputTextBg"];
    [self.inputTextField addSubview:self.inputTextBgView];
    [self.inputTextField sendSubviewToBack:self.inputTextBgView];
    [self.bottomContainerView addSubview:self.inputTextField];
    [self.bottomContainerView bringSubviewToFront:self.inputTextField];
    
    self.closeButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth - 2*kPaddingLeft- kCloseButtonWidth, 0, kCloseButtonWidth + 2*kPaddingLeft, kInputTextFieldHeight)];
    [self.closeButtonContainer yh_setCenterY:self.inputTextField.yh_centerY];
    UITapGestureRecognizer *closeGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoard:)];
    [self.closeButtonContainer addGestureRecognizer:closeGesture];
    self.closeButtonContainer.userInteractionEnabled = YES;
    self.closeButtonContainer.hidden = YES;
    self.keyBoardCloseButton = [[UIButton alloc] initWithFrame:CGRectMake(kPaddingLeft, 0, kCloseButtonWidth, kCloseButtonWidth)];
    [self.keyBoardCloseButton setImage:[UIImage imageNamed:@"keyBoardClose"] forState:UIControlStateNormal];
    self.keyBoardCloseButton.userInteractionEnabled = NO;
    [self.keyBoardCloseButton yh_setCenterY:self.inputTextField.yh_centerY];
    [self.closeButtonContainer addSubview:self.keyBoardCloseButton];
    [self.bottomContainerView addSubview:self.closeButtonContainer];
    
    [self.view addSubview:self.bottomContainerView];
    
    self.messageTip = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - kMessageTipWidth - kMessageTipLeftEdge, self.barrageTableView.yh_bottom - kMessageTipHeight, kMessageTipWidth, kMessageTipHeight)];
    self.messageTip.image = [UIImage imageNamed:@"message_Tip"];
    self.messageTip.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(newsBubbleClick:)];
    tapGR.numberOfTapsRequired = 1;
    [self.messageTip addGestureRecognizer:tapGR];
    
    self.messageArrow = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 10, 6)];
    self.messageArrow.image = [UIImage imageNamed:@"messageArrow"];
    [self.messageTip addSubview:self.messageArrow];
    
    [self.newsBubble setFrame:CGRectMake(CGRectGetMaxX(self.messageArrow.frame) + 5, 0, kMessageTipWidth - CGRectGetMaxX(self.messageArrow.frame) - 10, kMessageTipHeight)];
    [self.messageTip addSubview:self.newsBubble];
    self.messageTip.hidden = YES;
    
    [self.view addSubview:self.messageTip];
}

-(void)closeKeyBoard:(UIButton *)sender
{
    [self.inputTextField resignFirstResponder];
}


#pragma mark - 

- (void)show
{
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.view];
    [[UIApplication sharedApplication].keyWindow.rootViewController addChildViewController:self];
}


- (void)insertItem:(NSDictionary *)itemdic
{
    [self.barrageTableView yhBarrageTableView:self.barrageTableView insertCellWithItem:itemdic];

}

- (void)reset
{
    [self.barrageTableView reset];
}

- (void)newsBubbleClick:(id)sender
{
    [self.barrageTableView startAutoScolling];
    [self.messageTip setHidden:YES];
}

#pragma mark - YH_BarrageTableViewScrollingDelegate

- (void)manualScrollingWithNewMsgCount:(NSString *)newMsgCount
{
    NSString *msgCount ;
    if ([newMsgCount integerValue] > 99) {
        msgCount = @"99+条信息";
    }else {
        msgCount = [NSString stringWithFormat:@"%ld条信息",(long)[newMsgCount integerValue]];
    }
    [self.newsBubble setText:msgCount];
    CGSize size = CGSizeMake(60,kMessageTipHeight);
    CGSize labelsize = [self.newsBubble sizeThatFits:size];
    [self.newsBubble setFrame:CGRectMake(25, (kMessageTipHeight - labelsize.height) / 2, labelsize.width, labelsize.height)];
    [self.messageTip yh_setWidth:CGRectGetMaxX(self.newsBubble.frame) + 10];
    [self.messageTip setHidden:NO];
}

- (void)autoScollingCallback
{
    [self.messageTip setHidden:YES];
}

#pragma mark - getters and setters

- (YH_BarrageTableView *)barrageTableView
{
    if (_barrageTableView == nil) {
        _barrageTableView = [[YH_BarrageTableView alloc]init];
        _barrageTableView.barrageScrollingDelegate = self;
    }
    
    return _barrageTableView;
}

- (HPGrowingTextView *)inputTextField
{
    if (_inputTextField == nil) {
        _inputTextField = [[HPGrowingTextView alloc]init];
        _inputTextField.isScrollable = NO;
        _inputTextField.returnKeyType = UIReturnKeySend;
        _inputTextField.backgroundColor = [UIColor clearColor];
        _inputTextField.placeholder = @"说点什么吧...";
        _inputTextField.font = [UIFont systemFontOfSize:15.0f];
        _inputTextField.textColor = [UIColor whiteColor];
        _inputTextField.minNumberOfLines = 1;
        _inputTextField.maxNumberOfLines = 1;
        _inputTextField.internalTextView.textContainer.maximumNumberOfLines = 1;
        _inputTextField.internalTextView.textContainer.lineBreakMode = NSLineBreakByTruncatingHead;
        
        _inputTextField.delegate = self;
    }
    
    return _inputTextField;
}

- (UILabel *)newsBubble
{
    if (_newsBubble == nil) {
        _newsBubble = [[UILabel alloc]init];
        _newsBubble.userInteractionEnabled = YES;
        _newsBubble.textAlignment = NSTextAlignmentCenter;
        _newsBubble.font = [UIFont fontOfSCWithSize:11];
        _newsBubble.textColor = [UIColor blackColor];
    }
    
    return _newsBubble;
}

#pragma mark - HPGrowingTextViewDelegate
- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return YES;
}

- (BOOL)growingTextViewShouldBeginEditing:(HPGrowingTextView *)growingTextView {
    return NO;
}

-(void)textViewDidChange:(HPGrowingTextView *)growingTextView
{

}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end