今天的代码是在昨天的基础上完成了,昨天主要完成了QQ主界面,今天完成的主要是聊天界面
聊天界面一共有三个部分,最上面是一个导航栏NavigationBar
中间界面是一个UITableViewCell
下端界面是一个UIView,分别由TextLable和两个Button组成
在输入框时写两个,一个作为显示界面,另一个作为输出界面
//
// Constants.h
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#ifndef QQ_Constants_h
#define QQ_Constants_h
typedef enum{
kHeaderViewDirectionRight,
kHeaderViewDirectionDown
}kHeaderViewDirection;
#endif
//
// SectionHeaderView.h
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Constants.h"
@protocol SectionHeaderDirectionDelegate <NSObject>
- (void)headerViewDirectionDidChanged:(kHeaderViewDirection)direction section:(NSInteger)section;
@end
@interface SectionHeaderView : UIView
@property (nonatomic, assign) NSInteger section;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, assign) kHeaderViewDirection direction;
@property (nonatomic, assign) id<SectionHeaderDirectionDelegate> delegate;
@end
//
// SectionHeaderView.m
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import "SectionHeaderView.h"
@interface SectionHeaderView ()
@property (nonatomic, strong) UIImageView *directionImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation SectionHeaderView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
//创建按钮
UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeSystem];
[bgButton setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[bgButton setBackgroundColor:[UIColor colorWithRed:226/255.0 green:242/255.0 blue:251/255.0 alpha:1]];
[bgButton addTarget:self action:@selector(buttonDidClicked) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:bgButton];
//创建剪头
self.directionImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 20, 20)];
_directionImageView.image = [UIImage imageNamed:@"right"];
[bgButton addSubview:_directionImageView];
//创建titleLabel
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 12, 200, 20)];
_titleLabel.text = @"绯闻女友 7/20";
_titleLabel.textAlignment = NSTextAlignmentLeft;
_titleLabel.font = [UIFont systemFontOfSize:13];
[bgButton addSubview:_titleLabel];
}
return self;
}
- (void)setTitle:(NSString *)title{
if (_title != title) {
_title = title;
}
//将文本显示到titleLabel上
self.titleLabel.text = _title;
}
- (void)setDirection:(kHeaderViewDirection)direction{
_direction = direction;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
if (_direction == kHeaderViewDirectionDown) {
//剪头向下
self.directionImageView.transform = CGAffineTransformMakeRotation(M_PI_2);
//self.directionImageView.transform = CGAffineTransformRotate(_directionImageView.transform, M_PI_2);
} else{
//剪头向右边
self.directionImageView.transform = CGAffineTransformMakeRotation(M_PI_2 * 4);
//self.directionImageView.transform = CGAffineTransformRotate(_directionImageView.transform, -M_PI_2);
}
[UIView commitAnimations];
}
- (void)buttonDidClicked{
self.direction = (_direction == kHeaderViewDirectionDown )? kHeaderViewDirectionRight : kHeaderViewDirectionDown;
//将当前的状态回调给viewController
if([_delegate respondsToSelector:@selector(headerViewDirectionDidChanged:section:)]){
[_delegate headerViewDirectionDidChanged:_direction section:_section];
}
}
@end
//
// ChatViewController.h
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import <UIKit/UIKit.h>
@class FriendsModel;//前向声明
@interface ChatViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
- (instancetype)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
friendModel:(FriendsModel *)model;
@end
//
// ChatViewController.m
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import "ChatViewController.h"
#import "FriendsModel.h"
@interface ChatViewController ()
@property (nonatomic, strong) FriendsModel *model;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UITextField *inputTextField;
@property (nonatomic, strong) UITextField *showingTextField;
@end
@implementation ChatViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
friendModel:(FriendsModel *)model{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.model = model;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self uiInitial];
//注册(监听)一个键盘弹出来的消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil];
//注册(监听)一个键盘隐藏的消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyboardShow:(NSNotification *)notifi{
//让inputTextField 作为第一响应者
[_inputTextField becomeFirstResponder];
//tableView上移
self.myTableView.frame = CGRectMake(0, 20 + 44, 320, 568 - 253 - 44 - 64);
//显示最后一行
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:29 inSection:0];
[self.myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
- (void)keyboardHidden:(NSNotification *)notifi{
//将输入框的内容 显示到 showingTextField上
_showingTextField.text = _inputTextField.text;
//tableView还原
self.myTableView.frame = CGRectMake(0, 64, 320, 568-64-44);
}
- (void)uiInitial{
UIImage *bgImage = [UIImage imageNamed:@"bg"];
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
bgImageView.image = bgImage;
[self.view addSubview:bgImageView];
self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, 568-64-44) style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource =self;
_myTableView.backgroundColor = [UIColor clearColor];
_myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:_myTableView];
//真正的输入视图
UIView *reallyInputOperationView = [self createMessageInputOperationView];
reallyInputOperationView.frame = CGRectMake(0, 0, 320, 44);
self.inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, 200, 24)];
_inputTextField.placeholder = @"message";
_inputTextField.borderStyle = UITextBorderStyleLine;
_inputTextField.delegate = self;
[reallyInputOperationView addSubview:_inputTextField];
//显示的输入视图
UIView *showingOperationView = [self createMessageInputOperationView];
[self.view addSubview:showingOperationView];
self.showingTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, 200, 24)];
_showingTextField.placeholder = @"message";
_showingTextField.borderStyle = UITextBorderStyleLine;
_showingTextField.inputAccessoryView = reallyInputOperationView;
[showingOperationView addSubview:_showingTextField];
}
- (void)setModel:(FriendsModel *)model{
if (_model != model) {
_model = model;
}
//设置导航栏的标题 为联系人的名字
self.title = _model.friendName;
}
#pragma mark -- TableDelegate&DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.text = [NSString stringWithFormat:@"row_%ld", indexPath.row];
return cell;
}
- (UIView *)createMessageInputOperationView{
//背景视图
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 568-44, 320, 44)];
[bgView setBackgroundColor:[UIColor lightTextColor]];
//笑脸
UIButton *faceButton = [UIButton buttonWithType:UIButtonTypeCustom];
[faceButton setFrame:CGRectMake(240, 7, 30, 30)];
[faceButton setBackgroundImage:[UIImage imageNamed:@"face"] forState:UIControlStateNormal];
[bgView addSubview:faceButton];
//add
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[addButton setFrame:CGRectMake(280, 7, 30, 30)];
[bgView addSubview:addButton];
return bgView;
}
#pragma mark -- UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//取消textfield的第一响应者
[_inputTextField resignFirstResponder];
[_showingTextField resignFirstResponder];
return YES;
}
#pragma mark -- UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//取消textfield的第一响应者
[_inputTextField resignFirstResponder];
[_showingTextField resignFirstResponder];
}
@end