现在的位置: 首页 > 综合 > 正文

仿QQ布局实例

2017年01月24日 ⁄ 综合 ⁄ 共 6362字 ⁄ 字号 评论关闭

仿QQ布局实例

- (UIImageView*)iconImageView

{
   

if
(_iconImageView==

nil
) {
       

_iconImageView
= [[UIImageViewalloc]init];
        [
self.contentViewaddSubview:_iconImageView];
    }
   

return

_iconImageView
;
}

- (UILabel*)timerLabel
{
   

if
(_timerLabel==

nil
) {
       

_timerLabel
= [[UILabelalloc]init];
       

_timerLabel
.textAlignment=
NSTextAlignmentCenter;
       

_timerLabel
.font= [UIFontsystemFontOfSize:13.0];
        [
self.contentViewaddSubview:_timerLabel];
    }
   

return

_timerLabel
;
}

- (UIButton*)textBtn
{
   

if
(_textBtn==

nil
) {
       

_textBtn
= [[UIButtonalloc]init];
       

_textBtn
.titleLabel.numberOfLines=
0;
       

_textBtn
.titleLabel.font=
[
UIFontsystemFontOfSize:13.0];
        [
self.contentViewaddSubview:_textBtn];
    }
   

return

_textBtn
;
}

- (void)setQqInfo:(SUNQQInfo*)qqInfo
{
   

_qqInfo
= qqInfo;
   
   

//

设置时间

   

self
.timerLabel.text=
qqInfo.
time;
   

CGFloat
timerW = [UIScreenmainScreen].bounds.size.width;
   

self
.timerLabel.frame=
CGRectMake(0,0,
timerW,
35);
   
   

//

设置头像

   

if
(qqInfo.type) {
       

CGFloat
imageX =kMargin;
       

CGFloat
imageY =CGRectGetMaxY(self.timerLabel.frame)
+

kMargin
;
       

self
.iconImageView.frame=
CGRectMake(imageX, imageY,kIconW,kIconH);
       

self
.iconImageView.image=
[
UIImageimageNamed:@"other"];
    }
else{
       

CGFloat
X = [UIScreenmainScreen].bounds.size.width-
kMargin
-kIconW;
       

CGFloat
Y =CGRectGetMaxY(self.timerLabel.frame)
+

10
;
       

self
.iconImageView.frame=
CGRectMake(X, Y,kIconW,kIconH);
       

self
.iconImageView.image=
[
UIImageimageNamed:@"me"];
       
    }

   
//

设置文字

   

NSDictionary
*dict =@{NSFontAttributeName:
[
UIFontsystemFontOfSize:13.0]};
   

CGRect
frameText = [qqInfo.textboundingRectWithSize:CGSizeMake(150,MAXFLOAT)options:NSStringDrawingUsesLineFragmentOriginattributes:dictcontext:nil];
    [
self.textBtnsetTitle:qqInfo.textforState:UIControlStateNormal];
   

self
.textBtn.contentEdgeInsets=
UIEdgeInsetsMake(20,20,20,20);
   

CGFloat
textY =CGRectGetMaxY(self.timerLabel.frame)
+

10
;
   

CGFloat
textW = frameText.size.width+
40;
   

CGFloat
textH = frameText.size.height+
40;
   

CGFloat
textX;
   

if
(qqInfo.type) {
        textX =

CGRectGetMaxX
(self.iconImageView.frame)
+

10
;
    }
else{
        textX = [
UIScreenmainScreen].bounds.size.width-
2*kMargin-
kIconW
- textW;
    }
   

self
.textBtn.frame=
CGRectMake(textX, textY, textW, textH);
   
   

//

设置行高

   

CGFloat
maxH =MAX(CGRectGetMaxY(self.iconImageView.frame),CGRectGetMaxY(self.textBtn.frame));
   

self
.cellHight= maxH +
kMargin;
   
   
   

//

设置聊天背景

   

if
(qqInfo.type) {
       

UIImage
*image = [UIImageimageNamed:@"chat_recive_nor"];
       

UIImage
*image1 = [imagestretchableImageWithLeftCapWidth:30 topCapHeight:30];
        [
self.textBtnsetBackgroundImage:image1forState:UIControlStateNormal];
        [
self.textBtnsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
    }
else{
       

UIImage
*image = [UIImageimageNamed:@"chat_send_nor"];
       

UIImage
*image1 = [imagestretchableImageWithLeftCapWidth:30topCapHeight:30];
        [
self.textBtnsetBackgroundImage:image1forState:UIControlStateNormal];
        [
self.textBtnsetTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];
    }
}


- (void)viewDidLoad
{
    [
superviewDidLoad];
NSLog(@"%@",self.qqs);
   

self
.tableView.dataSource=

self
;
   

self
.tableView.delegate=

self
;
   

//

去除分割线

   

self
.tableView.separatorStyle=
UITableViewCellSeparatorStyleNone;
   

//

设置背景图片

   

self
.tableView.backgroundColor=
[
UIColorcolorWithRed:224/255.0green:224/255.0blue:224/255.0alpha:1.0];
   

//

界面不允许被点击

   

self
.tableView.allowsSelection=

NO
;
   
   

//

监听键盘的通知

    [[
NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillChangeFrameNotificationobject:nil];
   
   

//

处理文本输入框

   

self
.inputView.leftView=
[[
UITextFieldalloc]initWithFrame:CGRectMake(0,0,5,0)];
   

self
.inputView.leftViewMode=
UITextFieldViewModeAlways;
   

self
.inputView.delegate=

self
;
}

- (void)keyboardWillChangeFrame:(NSNotification*)noti
{
   

/**
     UIKeyboardFrameBeginUserInfoKey = NSRect: {{0, 480}, {320, 216}};
     UIKeyboardCenterBeginUserInfoKey = NSPoint: {160, 588};
     UIKeyboardFrameEndUserInfoKey = NSRect: {{0, 264}, {320, 216}};
     UIKeyboardCenterEndUserInfoKey = NSPoint: {160, 372};
     UIKeyboardAnimationDurationUserInfoKey = 0.25;
     UIKeyboardFrameChangedByUserInteraction = 0;
     UIKeyboardBoundsUserInfoKey = NSRect: {{0, 0}, {320, 216}};
     UIKeyboardAnimationCurveUserInfoKey = 7;
     }
     */

   
   

NSLog
(@"%@",noti.userInfo);
   

//

设置窗口的颜色

   

self
.view.window.backgroundColor=

self
.tableView.backgroundColor;
   
   

//

取出键盘动画的时间

   

CGFloat
duration= [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
   
   

//

取得键盘最后的
frame
   

CGRect
keyboardFrame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
   
   

//

计算控制器移动的距离

   

CGFloat
tansformY = keyboardFrame.origin.y-

self
.view.frame.size.height;
   
   

//

执行动画

    [
UIViewanimateWithDuration:durationanimations:^{
       

self
.view.transform=
CGAffineTransformMakeTranslation(0,
tansformY);
    }];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView*)scrollView
{
   

//

关闭键盘

    [
self.viewendEditing:YES];
}

- (void)dealloc
{
    [[
NSNotificationCenterdefaultCenter]removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [
superdidReceiveMemoryWarning];
   

// Dispose of any resources that can be recreated.

}

//设置状态栏的可见性
- (
BOOL)prefersStatusBarHidden
{
   

return

YES
;
}

#pragma mark -实现代理的方法
- (CGFloat)tableView:(UITableView*)tableView
heightForRowAtIndexPath:(
NSIndexPath*)indexPath
{
   

SUNQQView
*qqView = [[SUNQQViewalloc]init];
    qqView.
qqInfo

self
.qqs[indexPath.row];
   

return
qqView.cellHight;
}

- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(
NSIndexPath*)indexPath
{
   

static

NSString
*ID =@"cell";
   

SUNQQView
*cell = [tableViewdequeueReusableCellWithIdentifier:ID];
   

if
(cell ==nil) {
        cell = [[
SUNQQViewalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];
    }
   

//

取出数据

    cell.
qqInfo=

self
.qqs[indexPath.row];
    cell.
backgroundColor= [UIColorclearColor];
   
   

return
cell;
}

- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(
NSInteger)section
{
   

return

self
.qqs.count;
}

- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
   

NSLog
(@"%@",textField);
   

//

添加模型

   

SUNQQInfo
*qqInfo = [[SUNQQInfoalloc]init];
    qqInfo.
type=
0;
    qqInfo.
time=
@"11:23";
    qqInfo.
text= textField.text;
    [
self.qqsaddObject:qqInfo];
   
   

//

刷新表格

    [
self.tableViewreloadData];
   
   

//

自动滚动到最后一行

   

NSIndexPath
*path = [NSIndexPathindexPathForRow:self.qqs.count-
1
inSection:0];
    [
self.tableViewscrollToRowAtIndexPath:pathatScrollPosition:UITableViewScrollPositionBottomanimated:YES];
   
   

//

清除文本框上的文字

   

self
.inputView.text=

nil
;
   

return

YES
;
}

抱歉!评论已关闭.