This repository was archived by the owner on Mar 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathViewController.m
More file actions
145 lines (107 loc) · 5.07 KB
/
ViewController.m
File metadata and controls
145 lines (107 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// ViewController.m
//
// Created by Alex Barinov
// Project home page: http://alexbarinov.github.com/UIBubbleTableView/
//
// This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
//
//
// Images used in this example by Petr Kratochvil released into public domain
// http://www.publicdomainpictures.net/view-image.php?image=9806
// http://www.publicdomainpictures.net/view-image.php?image=1358
//
#import "ViewController.h"
#import "UIBubbleTableView.h"
#import "UIBubbleTableViewDataSource.h"
#import "NSBubbleData.h"
@interface ViewController ()
{
IBOutlet UIBubbleTableView *bubbleTable;
IBOutlet UIView *textInputView;
IBOutlet UITextField *textField;
NSMutableArray *bubbleData;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSBubbleData *heyBubble = [NSBubbleData dataWithText:@"Hey, halloween is soon" date:[NSDate dateWithTimeIntervalSinceNow:-300] type:BubbleTypeSomeoneElse];
heyBubble.avatar = [UIImage imageNamed:@"avatar1.png"];
NSBubbleData *photoBubble = [NSBubbleData dataWithImage:[UIImage imageNamed:@"halloween.jpg"] date:[NSDate dateWithTimeIntervalSinceNow:-290] type:BubbleTypeSomeoneElse];
photoBubble.avatar = [UIImage imageNamed:@"avatar1.png"];
NSBubbleData *replyBubble = [NSBubbleData dataWithText:@"Wow.. Really cool picture out there. iPhone 5 has really nice camera, yeah?" date:[NSDate dateWithTimeIntervalSinceNow:-5] type:BubbleTypeMine];
replyBubble.avatar = nil;
bubbleData = [[NSMutableArray alloc] initWithObjects:heyBubble, photoBubble, replyBubble, nil];
bubbleTable.bubbleDataSource = self;
// The line below sets the snap interval in seconds. This defines how the bubbles will be grouped in time.
// Interval of 120 means that if the next messages comes in 2 minutes since the last message, it will be added into the same group.
// Groups are delimited with header which contains date and time for the first message in the group.
bubbleTable.snapInterval = 120;
// The line below enables avatar support. Avatar can be specified for each bubble with .avatar property of NSBubbleData.
// Avatars are enabled for the whole table at once. If particular NSBubbleData misses the avatar, a default placeholder will be set (missingAvatar.png)
bubbleTable.showAvatars = YES;
// Uncomment the line below to add "Now typing" bubble
// Possible values are
// - NSBubbleTypingTypeSomebody - shows "now typing" bubble on the left
// - NSBubbleTypingTypeMe - shows "now typing" bubble on the right
// - NSBubbleTypingTypeNone - no "now typing" bubble
bubbleTable.typingBubble = NSBubbleTypingTypeSomebody;
[bubbleTable reloadData];
// Keyboard events
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark - UIBubbleTableViewDataSource implementation
- (NSUInteger)rowsForBubbleTable:(UIBubbleTableView *)tableView
{
return [bubbleData count];
}
- (NSBubbleData *)bubbleTableView:(UIBubbleTableView *)tableView dataForRow:(NSInteger)row
{
return [bubbleData objectAtIndex:row];
}
#pragma mark - Keyboard events
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.2f animations:^{
CGRect frame = textInputView.frame;
frame.origin.y -= kbSize.height;
textInputView.frame = frame;
frame = bubbleTable.frame;
frame.size.height -= kbSize.height;
bubbleTable.frame = frame;
}];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.2f animations:^{
CGRect frame = textInputView.frame;
frame.origin.y += kbSize.height;
textInputView.frame = frame;
frame = bubbleTable.frame;
frame.size.height += kbSize.height;
bubbleTable.frame = frame;
}];
}
#pragma mark - Actions
- (IBAction)sayPressed:(id)sender
{
bubbleTable.typingBubble = NSBubbleTypingTypeNobody;
NSBubbleData *sayBubble = [NSBubbleData dataWithText:textField.text date:[NSDate dateWithTimeIntervalSinceNow:0] type:BubbleTypeMine];
[bubbleData addObject:sayBubble];
[bubbleTable reloadData];
textField.text = @"";
[textField resignFirstResponder];
}
@end