2011-10-11 20 views
11

我正在構建一個應用程序,與我已構建的一個Web應用程序一起使用,該應用程序具有線程評論系統。爲iOS設計一個線程評論系統

我想知道構建這個線程視圖的最佳方法是什麼。是否有一種相對簡單的方法來建立手風琴風格控制?

我真的很喜歡外國人藍色的應用程序是怎麼做的,以及UI & UX非常流暢:

enter image description here

  • 沒有人有任何想法如何,這些都是建立?
  • 將定製UIViews添加爲子視圖是最好的方法?如果是這樣,你將如何實現'崩潰'式功能?

回答

8

我建議創建一個UIView子類來包含每個評論。它應該有一個切換按鈕來展開/摺疊。在切換打開我會設置框架大小的內容(加上任何填充)的大小。它將包含一系列子註釋,對於每個將在擴展時將UIView子類添加到自身(並在摺疊時刪除)的組件,它們最初將被摺疊(因此只顯示爲切換按鈕)。摺疊恰恰相反,刪除子視圖,將框架設置爲切換按鈕的高度(加上填充)

由於每個評論視圖都知道其大小,因此可以將整個事件放在具有內容大小的UIScrollView中設置爲允許滾動的註釋視圖大小的總和,而不管展開/收縮的性質。

這種想法部分實現:

Comment.h

#import <Foundation/Foundation.h> 

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

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

@end 

Comment.m

#import "Comment.h" 

@implementation Comment 
@synthesize comment, subComments; 

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

@end 

CommentView.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 

CommentView.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; 
} 
+0

哇,謝謝馬特。這很棒。我注意到,當我把所有這些都放在一個示例項目中時,只顯示了第一條主要評論。單擊子註釋的指示器不會執行任何操作。任何想法爲什麼? – barfoon

+0

'expand'方法永遠不會被註釋爲兒童評論,我懷疑它與視圖的框架有關 – barfoon

-1

是這樣的不只是一個定製的內置HTML頁面顯示評論的WebView?