iOS聊天室广播的显示

@interface ChatRoomSpecialMessageView : UIView
- (instancetype)initWithFrame:(CGRect)frame;
/**
 *  使用说明
 *  @param message          弹窗message
 *  @return 弹窗
 */
-(void)buildViewWithMessage:(NSString*)message;
@end

@implementation WSChatRoomSpecialMessageView
{
    NSMutableArray *messageArr;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColorFromRGB(0xFC9274);
        messageArr = [[NSMutableArray alloc] init];
    }
return self;
}
-(void)buildViewWithMessage:(NSString*)message{
    if (messageArr.count>0) {
        //如果还有消息没消失,则等待4秒后再显示
        double delayInSeconds = 4.0*messageArr.count;
        dispatch_time_t popTime =     dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            self.hidden = NO;
            [self buildViewWithMessage:message];
        });
        return;
    }

    UILabel *messageLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height)];
    messageLbl.text = message;
    messageLbl.textColor = [UIColor whiteColor];
    messageLbl.font = [UIFont systemFontOfSize:12];
    messageLbl.textAlignment = NSTextAlignmentCenter;
    [self addSubview:messageLbl];
    [messageArr addObject:messageLbl];

    double delayInSeconds = 4.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [messageLbl removeFromSuperview];
        [messageArr removeObject:messageLbl];
        if (messageArr.count==0) {
            self.hidden = YES;
        }
    });
}
@end

你可能感兴趣的:(iOS聊天室广播的显示)