云豹短视频app源码中,标签选择功能的实现
在国内任意短视频app中,都能够看到“贴标签功能”,云豹短视频app源码作为紧跟行业潮流的成品商业源码,自然也有该功能的实现,在云豹短视频app源码中,标签选择功能的实现要求是这样的——
短视频app源码标签选择功能的实现要求:
1、 所有标签显示在下方,被选择的标签出现在上方,且在下方显示“被选中状态”,被选中的标签再次点击可取消,同时底部取消选中状态。
2、 标签的宽度自适应,显示不全自行判断折行展示
3、 若显示标签过多,超出上下边界,则支持滑动展现
实现效果图如下:
通过短视频源码进行功能实现:
-(CGSize)intrinsicContentSize { if (!self.tags.count) { return CGSizeZero; } NSArray *subviews = self.subviews; UIView *previousView = nil; CGFloat topPadding = self.padding.top; CGFloat bottomPadding = self.padding.bottom; CGFloat leftPadding = self.padding.left; CGFloat rightPadding = self.padding.right; CGFloat itemSpacing = self.interitemSpacing; CGFloat lineSpacing = self.lineSpacing; CGFloat currentX = leftPadding; CGFloat intrinsicHeight = topPadding; CGFloat intrinsicWidth = leftPadding; if (!self.singleLine && self.preferredMaxLayoutWidth > 0) { NSInteger lineCount = 0; for (UIView *view in subviews) { CGSize size = view.intrinsicContentSize; // 宽度和高度通过参数的0或者非0来进行赋值 CGFloat width = self.isIntrinsicWidth?self.regularWidth:size.width; CGFloat height = self.isIntrinsicHeight?self.regularHeight:size.height; if (previousView) { // CGFloat width = size.width; currentX += itemSpacing; if (currentX + width + rightPadding <= self.preferredMaxLayoutWidth) { currentX += width; } else { lineCount ++; currentX = leftPadding + width; intrinsicHeight += height; } } else { lineCount ++; intrinsicHeight += height; currentX += width; } previousView = view; intrinsicWidth = MAX(intrinsicWidth, currentX + rightPadding); } intrinsicHeight += bottomPadding + lineSpacing * (lineCount - 1); } else { for (UIView *view in subviews) { CGSize size = view.intrinsicContentSize; intrinsicWidth += self.isIntrinsicWidth?self.regularWidth:size.width; } intrinsicWidth += itemSpacing * (subviews.count - 1) + rightPadding; intrinsicHeight += ((UIView *)subviews.firstObject).intrinsicContentSize.height + bottomPadding; } return CGSizeMake(intrinsicWidth, intrinsicHeight); } - (void)layoutSubviews { if (!self.singleLine) { self.preferredMaxLayoutWidth = self.frame.size.width; } [super layoutSubviews]; [self layoutTags]; } #pragma mark - Custom accessors - (NSMutableArray *)tags { if(!_tags) { _tags = [NSMutableArray array]; } return _tags; } - (void)setPreferredMaxLayoutWidth: (CGFloat)preferredMaxLayoutWidth { if (preferredMaxLayoutWidth != _preferredMaxLayoutWidth) { _preferredMaxLayoutWidth = preferredMaxLayoutWidth; _didSetup = NO; [self invalidateIntrinsicContentSize]; } } #pragma mark - Private - (void)layoutTags { if (self.didSetup || !self.tags.count) { return; } NSArray *subviews = self.subviews; UIView *previousView = nil; CGFloat topPadding = self.padding.top; CGFloat leftPadding = self.padding.left; CGFloat rightPadding = self.padding.right; CGFloat itemSpacing = self.interitemSpacing; CGFloat lineSpacing = self.lineSpacing; CGFloat currentX = leftPadding; if (!self.singleLine && self.preferredMaxLayoutWidth > 0) { for (UIView *view in subviews) { CGSize size = view.intrinsicContentSize; CGFloat width1 = self.isIntrinsicWidth?self.regularWidth:size.width; CGFloat height1 = self.isIntrinsicHeight?self.regularHeight:size.height; if (previousView) { // CGFloat width = size.width; currentX += itemSpacing; if (currentX + width1 + rightPadding <= self.preferredMaxLayoutWidth) { view.frame = CGRectMake(currentX, CGRectGetMinY(previousView.frame), width1, height1); currentX += width1; } else { CGFloat width = MIN(width1, self.preferredMaxLayoutWidth - leftPadding - rightPadding); view.frame = CGRectMake(leftPadding, CGRectGetMaxY(previousView.frame) + lineSpacing, width, height1); currentX = leftPadding + width; } } else { CGFloat width = MIN(width1, self.preferredMaxLayoutWidth - leftPadding - rightPadding); view.frame = CGRectMake(leftPadding, topPadding, width, height1); currentX += width; } previousView = view; } } else { for (UIView *view in subviews) { CGSize size = view.intrinsicContentSize; view.frame = CGRectMake(currentX, topPadding, self.isIntrinsicWidth?self.regularWidth:size.width, self.isIntrinsicHeight?self.regularHeight:size.height); currentX += self.isIntrinsicWidth?self.regularWidth:size.width; previousView = view; } } self.didSetup = YES;
由此,短视频app源码中标签选择功能的实现教程到此结束,更多相关教程请关注云豹科技,如需购买短视频app源码,欢迎联系云豹科技,云豹科技不断更新短视频app源码,更新日志官网可查。
声明:以上内容为云豹科技作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任www.yunbaokj.com