近几年直播一火再火,现在的直播已经不再是主播们唱唱歌了,连老罗都已经开始直播带货,一再刷新抖音直播在线人数了。
但今天我们不是来说怎么做直播的,是来看看直播场景里的聊天消息界面是如何实现的。
估计很多人要失望了????
要实现聊天消息界面,不可不用 UITableView。当几年前我开始自学开发 iOS APP 时,我就开始使用 AsyncDisplayKit,现在已经更名为:Texture。
Keeps the most complex iOS user interfaces smooth and responsive.
Texture is an iOS framework built on top of UIKit that keeps even the most complex user interfaces smooth and responsive. It was originally built to make Facebook’s Paper possible, and goes hand-in-hand with pop’s physics-based animations — but it’s just as powerful with UIKit Dynamics and conventional app designs. More recently, it was used to power Pinterest’s app rewrite.
As the framework has grown, many features have been added that can save developers tons of time by eliminating common boilerplate style structures common in modern iOS apps. If you’ve ever dealt with cell reuse bugs, tried to performantly preload data for a page or scroll style interface or even just tried to keep your app from dropping too many frames you can benefit from integrating Texture.
参考Texture 官网
以后把每次用到的 Nodes 心得写出来,今天来说一说使用 ASTableNode
。
创建 ASTableNode
和 UITableView
一样,比较简单。
@interface TestViewController () <ASTableDataSource, ASTableDelegate> @property (nonatomic, strong) ASTableNode *tableNode; @property (nonatomic, strong) NSMutableArray *dataSource; @end
初始化:
_tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain]; _tableNode.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _tableNode.backgroundColor = [UIColor.clearColor colorWithAlphaComponent:0.0]; _tableNode.view.separatorStyle = UITableViewCellSeparatorStyleNone; [self.view addSubnode:_tableNode]; _tableNode.frame = CGRectMake(0, self.view.bounds.size.height - 300, 300, 200); // 填充测试数据 _dataSource = [NSMutableArray arrayWithArray:@[ @{@"type": @"TEXT", @"text": @"你好", @"nickname": @"yemeishu"}, @{@"type": @"TEXT", @"text": @"你好,这个主播不错哦~", @"nickname": @"yemeishu"}, @{@"type": @"TEXT", @"text": @"现在直播还可以带货了", @"nickname": @"yemeishu"} ]]; _tableNode.delegate = self; _tableNode.dataSource = self; _tableNode.view.allowsSelection = NO;
和 UITableView
一样,实现 dataSorce
和 delegate
(这里暂时不写对 Node 操作):
#pragma mark - ASTableNode - (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *message = self.dataSource[(NSUInteger) indexPath.row]; return ^{ return [[MessageNode alloc] initWithMessage: message]; }; } - (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; }
现在可以创建 ASCellNode 子类。
ASCellNode, as you may have guessed, is the cell class of Texture. Unlike the various cells in UIKit, ASCellNode can be used with ASTableNodes, ASCollectionNodes and ASPagerNodes, making it incredibly flexible.
ASCellNode 核心函数主要有四个,我们的重点在 layoutSpecThatFits
上。
-init – Thread safe initialization.
-layoutSpecThatFits: – Return a layout spec that defines the layout of your cell.
-didLoad – Called on the main thread. Good place to add gesture recognizers, etc.
-layout – Also called on the main thread. Layout is complete after the call to super which means you can do any extra tweaking you need to do.
具体 MessageNode
类直接看代码,只要将每个人聊天的信息发给 MessageNode
填充内容:
@interface MessageNode : ASCellNode - (instancetype)initWithMessage:(NSDictionary *)message; @end
这里为了简单实现效果,只是显示消息者姓名和消息内容。
#import "MessageNode.h" @interface ZJMessageNode() @property (strong, nonatomic) ASButtonNode *textNode; @end @implementation MessageNode { } - (instancetype)initWithMessage:(NSDictionary *)message { self = [super init]; if (self) { _textNode = [[ASButtonNode alloc] init]; NSString* nickname = @""; NSString* text = @""; if ([message[@"type"] isEqual: @"TEXT"]) { nickname = [NSString stringWithFormat:@"[%@]:",message[@"nickname"]]; text = message[@"text"]; } else { nickname = @"其他人"; text = @"其他消息"; } NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:@""]; NSAttributedString* nameString = [[NSAttributedString alloc] initWithString:nickname attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: UIColorMakeWithHex(@"#FF9900") }]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 5.0; NSAttributedString* textString = [[NSAttributedString alloc] initWithString: text attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: UIColor.ZJ_tintColor, NSParagraphStyleAttributeName: paragraphStyle }]; [string appendAttributedString:nameString]; [string appendAttributedString:textString]; _textNode.titleNode.attributedText = string; _textNode.titleNode.maximumNumberOfLines = 3; _textNode.backgroundImageNode.image = [UIImage as_resizableRoundedImageWithCornerRadius:8 cornerColor:UIColor.clearColor fillColor: [UIColor colorWithRed:26/255.0 green:26/255.0 blue:26/255.0 alpha:0.5]];这篇关于Texture ASTableNode 实现iOS直播聊天消息界面的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!