再用Udp--聊天工具
加入AsynScoket ( 要用到里边的Udp) 导入CFNetwork.framework库
------顶上加UINavigationControlle-------------
1.第一个页面ViewController中
UI ipField输入框-----输入对方ip
Event beginChat按钮点击事件-----跳转到第二个页面,并把ip字符串传过去
2.第二个页面ChatViewController中
UI _tableView表单-----显示双方对话
sendView------自己操作模块
--------sendField输入框-----输入对话
--------rightButton按钮-----发送对话
--------leftButton按钮-----收回键盘
Scoket sendSocket-----发送端 (F++)
recvSocket-----接收端
Data dataArray
Noti UIKeyboardWillShowNotification
UIKeyboardWillHideNotification
Event sendText-----发送对话
resignKeyBoard-----让键盘消失
delegate UITableViewDelegate,UITableViewDataSource,AsyncUdpSocketDelegate
----UITableViewDelegate----- (CGFloat)tableView: heightForRowAtIndexPath:
----UITableViewDataSource------ (NSInteger)tableView: numberOfRowsInSection:
------- (UITableViewCell*)tableView: cellForRowAtIndexPath:
----AsyncUdpSocketDelegate------(BOOL)onUdpSocket: didReceiveData: withTag: fromHost: port:
3.封装ChatCell
UI leftBubble-------左对话UIImageView
rightBubble
leftLabel-------左对话
rightLabel
4.代码来了-----viewController不写了
ChatViewController.h
1 #import <UIKit/UIKit.h> 2 #import "AsyncUdpSocket.h" 3 4 @interface ChatViewController : UIViewController 5 <UITableViewDelegate,UITableViewDataSource,AsyncUdpSocketDelegate> 6 { 7 UITableView* _tableView; 8 NSMutableArray* dataArray; 9 UIView* sendView; 10 UITextField* sendField; 11 AsyncUdpSocket* sendSocket; 12 AsyncUdpSocket* recvSocket; 13 } 14 15 @property (nonatomic,retain)NSString* ipStr; 16 17 @end
ChatViewController.m
1 #import "ChatViewController.h" 2 #import "ChatCell.h" 3 4 @implementation ChatViewController 5 @synthesize ipStr; 6 7 #pragma mark - viewDidLoad firstEvent 8 -(void)initFplus 9 { 10 sendSocket = [[AsyncUdpSocket alloc] initWithDelegate:self]; 11 recvSocket = [[AsyncUdpSocket alloc] initWithDelegate:self]; 12 [sendSocket bindToPort:5888 error:nil]; 13 [recvSocket bindToPort:5999 error:nil]; 14 [recvSocket receiveWithTimeout:-1 tag:100]; 15 16 dataArray = [[NSMutableArray alloc] init]; 17 } 18 -(void)initFTableView 19 { 20 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416 - 40)]; 21 _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 22 _tableView.delegate = self; 23 _tableView.dataSource = self; 24 [self.view addSubview:_tableView]; 25 [_tableView release]; 26 } 27 -(void)initFSendView 28 { 29 sendView = [[UIView alloc] initWithFrame:CGRectMake(0, 416-40, 320, 40)]; 30 sendView.backgroundColor = [UIColor grayColor]; 31 [self.view addSubview:sendView]; 32 [sendView release]; 33 34 sendField = [[UITextField alloc] initWithFrame:CGRectMake(50, 5, 220, 30)]; 35 sendField.borderStyle = UITextBorderStyleRoundedRect; 36 [sendView addSubview:sendField]; 37 [sendField release]; 38 39 UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 40 leftButton.frame = CGRectMake(5, 5, 30, 30); 41 [sendView addSubview:leftButton]; 42 [leftButton addTarget:self action:@selector(resignKeyBoard) forControlEvents:UIControlEventTouchUpInside]; 43 44 UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 45 rightButton.frame = CGRectMake(275, 5, 30, 30); 46 [sendView addSubview:rightButton]; 47 [rightButton addTarget:self action:@selector(sendText) forControlEvents:UIControlEventTouchUpInside]; 48 } 49 -(void)addFNotification 50 { 51 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 52 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 53 } 54 55 #pragma mark - secondEvent 56 - (void)sendText{ 57 NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithCapacity:0]; 58 [dic setObject:@"1" forKey:@"isSelf"]; 59 [dic setObject:sendField.text forKey:@"content"]; 60 [dataArray addObject:dic]; 61 [_tableView reloadData]; 62 [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:dataArray.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 63 64 NSData* data = [sendField.text dataUsingEncoding:NSUTF8StringEncoding]; 65 [sendSocket sendData:data toHost:ipStr port:5999 withTimeout:60 tag:100]; 66 67 sendField.text = @""; 68 } 69 - (void)resignKeyBoard{ 70 [sendField resignFirstResponder]; 71 } 72 - (void)keyboardWillShow:(NSNotification*)noti{ 73 NSDictionary* dic = noti.userInfo; 74 CGSize size = [[dic objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 75 76 [UIView beginAnimations:nil context:nil]; 77 [UIView setAnimationDuration:0.25]; 78 _tableView.frame = CGRectMake(0, 0, 320, 416 - 40 - size.height); 79 sendView.frame = CGRectMake(0, 416 - 40 - size.height, 320, 40); 80 [UIView commitAnimations]; 81 } 82 - (void)keyboardWillHide:(NSNotification*)noti{ 83 [UIView beginAnimations:nil context:nil]; 84 [UIView setAnimationDuration:0.25]; 85 _tableView.frame = CGRectMake(0, 0, 320, 416 - 40); 86 sendView.frame = CGRectMake(0, 416 - 40, 320, 40); 87 [UIView commitAnimations]; 88 } 89 90 91 #pragma mark - viewDidLoad 92 - (void)viewDidLoad 93 { 94 [super viewDidLoad]; 95 //socket data 96 [self initFplus]; 97 [self initFTableView]; 98 [self initFSendView]; 99 [self addFNotification]; 100 101 } 102 103 104 105 #pragma mark - AsyncUdpSocketDelegate 106 //接收消息 107 - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{ 108 NSString* str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; 109 NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithCapacity:0]; 110 [dic setObject:@"0" forKey:@"isSelf"]; 111 [dic setObject:str forKey:@"content"]; 112 [dataArray addObject:dic]; 113 [_tableView reloadData]; 114 [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:dataArray.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 115 [sock receiveWithTimeout:-1 tag:100]; 116 return YES; 117 } 118 119 #pragma mark - UITableViewDelegate 120 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 121 //调整为自动调高 122 NSDictionary* dic = [dataArray objectAtIndex:indexPath.row]; 123 NSString* content = [dic objectForKey:@"content"]; 124 CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:CGSizeMake(250, 1000) lineBreakMode:UILineBreakModeCharacterWrap]; 125 126 return 35.0 + size.height; 127 } 128 129 #pragma mark - UITableViewDataSource 130 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 131 return dataArray.count; 132 } 133 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 134 ChatCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"]; 135 if (cell == nil) { 136 cell = [[[ChatCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ID"] autorelease]; 137 } 138 139 NSDictionary* dic = [dataArray objectAtIndex:indexPath.row]; 140 NSString* isSelf = [dic objectForKey:@"isSelf"]; 141 NSString* content = [dic objectForKey:@"content"]; 142 CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:CGSizeMake(250, 1000) lineBreakMode:UILineBreakModeCharacterWrap]; 143 144 if ([isSelf isEqualToString:@"1"]) { 145 cell.leftBubble.hidden = YES; 146 cell.rightBubble.hidden = NO; 147 148 cell.rightLabel.text = content; 149 cell.rightLabel.frame = CGRectMake(5, 5, size.width, size.height); 150 cell.rightBubble.frame = CGRectMake(320-20-5-size.width, 5, 20 + size.width, 15 + size.height); 151 } else { 152 cell.leftBubble.hidden = NO; 153 cell.rightBubble.hidden = YES; 154 155 cell.leftLabel.text = content; 156 cell.leftLabel.frame = CGRectMake(10, 5, size.width, size.height); 157 cell.leftBubble.frame = CGRectMake(5, 5, 20 + size.width, 15 + size.height); 158 } 159 160 return cell; 161 } 162 163 @end
ChatCall.h
1 #import <UIKit/UIKit.h> 2 3 @interface ChatCell : UITableViewCell 4 5 @property (nonatomic,retain)UIImageView* leftBubble; 6 @property (nonatomic,retain)UIImageView* rightBubble; 7 @property (nonatomic,retain)UILabel* leftLabel; 8 @property (nonatomic,retain)UILabel* rightLabel; 9 10 @end
ChatCall.m
1 #import "ChatCell.h" 2 3 @implementation ChatCell 4 @synthesize leftLabel,leftBubble; 5 @synthesize rightLabel,rightBubble; 6 7 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 8 { 9 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 10 if (self) { 11 [self performSelector:@selector(makeView)]; 12 } 13 return self; 14 } 15 16 //45 17 - (void)makeView{ 18 //左气泡 19 leftBubble = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 35)]; 20 UIImage* image = [UIImage imageNamed:@"Private letter_List_1.png"]; 21 leftBubble.image = [image stretchableImageWithLeftCapWidth:12 topCapHeight:20]; 22 [self.contentView addSubview:leftBubble]; 23 [leftBubble release]; 24 25 //右气泡 26 rightBubble = [[UIImageView alloc] initWithFrame:CGRectMake(320-20-5, 5, 20, 35)]; 27 image = [UIImage imageNamed:@"Private letter_List_2.png"]; 28 rightBubble.image = [image stretchableImageWithLeftCapWidth:10 topCapHeight:20]; 29 [self.contentView addSubview:rightBubble]; 30 [rightBubble release]; 31 32 //左label 33 leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 5, 5)]; 34 leftLabel.backgroundColor = [UIColor clearColor]; 35 leftLabel.font = [UIFont systemFontOfSize:13.0]; 36 leftLabel.numberOfLines = 0; 37 leftLabel.lineBreakMode = UILineBreakModeCharacterWrap; 38 [leftBubble addSubview:leftLabel]; 39 [leftLabel release]; 40 41 //右label 42 rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 5, 5)]; 43 rightLabel.backgroundColor = [UIColor clearColor]; 44 rightLabel.font = [UIFont systemFontOfSize:13.0]; 45 rightLabel.numberOfLines = 0; 46 rightLabel.lineBreakMode = UILineBreakModeCharacterWrap; 47 [rightBubble addSubview:rightLabel]; 48 [rightLabel release]; 49 } 50 51 @end