iphone - 为 iOS 设计线程评论系统

标签 iphone ios cocoa-touch

我正在构建一个应用程序来配合我构建的网络应用程序,它具有线程评论系统。

我想知道构建此线程 View 的最佳方法是什么。有没有相对简单的方法来构建 Accordion 样式控件?

我真的很喜欢 Alien Blue 应用程序的处理方式,而且 UI 和 UX 非常流畅:

enter image description here

  • 有人知道这些是如何构建的吗?
  • 将自定义 UIView 添加为 subview 是否是最佳方法?如果是这样,您将如何实现“折叠”样式的功能?

最佳答案

我建议创建一个 UIView 子类来包含每条评论。它应该有一个切换按钮来展开/折叠。在切换打开时,我将框架大小设置为内容的大小(加上任何填充)。它将包含一组子注释,对于每个子注释,您将在展开时将 UIView 子类添加到自身(并在折叠时删除),这些子注释最初会折叠(因此仅显示为切换按钮)。折叠只是相反,删除 subview ,将框架设置为切换按钮的高度(加上填充)

因为每个评论 View 都知道它的大小,所以您可以将整个内容放在一个 UIScrollView 中,并将内容大小设置为评论 View 大小的总和,无论扩展/收缩性质如何都允许滚动。

这个想法的部分实现:

评论.h

#import <Foundation/Foundation.h>

@interface Comment : NSObject {
    NSMutableArray* subComments;
    NSString* comment;
}

@property (nonatomic, retain) NSMutableArray* subComments;
@property (nonatomic, retain) NSString* comment;

@end

评论.m

#import "Comment.h"

@implementation Comment 
@synthesize comment, subComments;

-(id)init
{
    self = [super init];
    if (self)
    {
        subComments = [[NSMutableArray alloc] init];
    }
    return self;
}

@end

评论 View .h

#import <UIKit/UIKit.h>

@interface CommentView : UIView {
    UIButton* toggle;
    NSMutableArray* subComments;
    NSString* commentText;
    UITextView* comment;
    BOOL expanded;
}
@property (strong, nonatomic) NSMutableArray* subComments;
@property (strong, nonatomic) NSString* commentText;


- (void) expand;
- (void) collapse;
- (void) toggleState;

@end

评论 View .m

#import "CommentView.h"


@implementation CommentView
@synthesize subComments,commentText;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    [self setBackgroundColor:[UIColor whiteColor]];
    expanded = NO;
    toggle = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [toggle setTitle:@"Toggle" forState:UIControlStateNormal];
    [toggle addTarget:self action:@selector(toggleState) forControlEvents:UIControlEventTouchUpInside];
    CGRect curentFrame = self.frame;
    curentFrame.size.height = toggle.frame.size.height + 10;
    [self setFrame:curentFrame];

    curentFrame = toggle.frame;
    curentFrame.origin.y = 5;
    curentFrame.origin.x = 5;
    [toggle setFrame:curentFrame];
    [self addSubview:toggle];
    [self collapse];

    return self;
}

- (void) toggleState
{
    if (expanded)
    {
        [self collapse];
    }
    else
    {
        [self expand];
    }
}

- (void) expand
{
    comment = [[UITextView alloc] init];
    [comment setEditable:NO];
    [comment setText:commentText];
    [self addSubview:comment];
    CGRect curentFrame = comment.frame;
    curentFrame.size.height = comment.contentSize.height;
    curentFrame.origin.x = toggle.frame.size.width + toggle.frame.origin.x + 10;
    curentFrame.size.width = self.frame.size.width - curentFrame.origin.x;
    curentFrame.origin.y = toggle.frame.size.height + toggle.frame.origin.y + 10;
    [comment setFrame:curentFrame];

    curentFrame = self.frame;
    curentFrame.size.height += comment.frame.size.height + 10;
    [self setFrame:curentFrame];
    float height = comment.frame.origin.y + comment.frame.size.height;
    for (NSObject* o in subComments)
    {
        CommentView* subComment = [[CommentView alloc] initWithFrame:CGRectMake(comment.frame.origin.x,height,0,self.frame.size.width)];
        [self addSubview:subComment];
        height += subComment.frame.size.height;
        curentFrame = self.frame;
        curentFrame.size.height += subComment.frame.size.height;
        [self setFrame:curentFrame];
        [self bringSubviewToFront:subComment];
    }
    expanded = YES;
}

- (void) collapse
{
    for (UIView* v in [self subviews])
    {
        [v removeFromSuperview];
    }

    CGRect curentFrame = self.frame;
    curentFrame.size.height = toggle.frame.size.height + 10;
    [self setFrame:curentFrame];

    curentFrame = toggle.frame;
    curentFrame.origin.y = 5;
    curentFrame.origin.x = 5;
    [toggle setFrame:curentFrame];
    [self addSubview:toggle];

    expanded = NO;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

ViewContoller.m(使用示例)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CommentView* mainCommentView = [[CommentView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

    Comment* mainComment = [[Comment alloc] init];
    [mainComment setComment: @"Lorem Ipsum 1"];

    Comment* sub1 = [[Comment alloc] init];
    [sub1 setComment: @"Lorem Ipsum 1-1"];
    Comment* sub11 = [[Comment alloc] init];
    [sub11 setComment: @"Lorem Ipsum 1-1-1"];
    [[sub1 subComments] addObject:sub11];

    Comment* sub2 = [[Comment alloc] init];
    [sub2 setComment: @"Lorem Ipsum 1-2"];
    Comment* sub12 = [[Comment alloc] init];
    [sub12 setComment: @"Lorem Ipsum 1-2-1"];
    [[sub2 subComments] addObject:sub12];

    [[mainComment subComments] addObject:sub1];
    [[mainComment subComments] addObject:sub2];

    [mainCommentView setCommentText:[mainComment comment]];
    [mainCommentView setSubComments:[mainComment subComments]];

    self.view = mainCommentView;
}

关于iphone - 为 iOS 设计线程评论系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7728412/

相关文章:

iphone - 当用户进入 UITableView 的编辑模式时如何更改披露样式?

objective-c - 我可以将自定义 UI 元素添加到 UIActionSheet 吗?

iphone - 测试与 facebook 集成的应用程序

iOS,全局处理tableview属性

objective-c - 如何避免 xcode 4.5 w/o ARC 出现僵尸错误?

ios - 为什么我的自定义表格单元格中的标签在单击行之前无法显示

ios - 在用户位置更新时停止 map 更新 - Google Maps iOS SDK

ios - UiTextView - 仅添加一行时的位置

iphone - GPUImageStillCamera 输出图像分辨率

ios - 在 ios 7 xcode 中单击 ScrollView 上方的按钮时如何滚动 ScrollView ?