IOS开发中构建聊天页面的情况

来源:爱站网时间:2021-01-11编辑:网友分享
说到聊天界面,相信大家都非常熟悉,而程序员搭建聊天页面要怎么操作呢?那么接下来爱站技术频道小编为大家讲解IOS开发中构建聊天页面的情况,一起跟随小编过来看看吧。

说到聊天界面,相信大家都非常熟悉,而程序员搭建聊天页面要怎么操作呢?那么接下来爱站技术频道小编为大家讲解IOS开发中构建聊天页面的情况,一起跟随小编过来看看吧。

由于工作需要,需要用到ios聊天页面,在网上搜了半天没有想要的,果断自己写一个,发个笔记

功能分析,模仿QQ聊天页面

输入框失去第一响应的情况:

1:点击页面

2:下滑页面

输入框成为第一响应的情况:

1:开始输入

2:上滑页面最底部

控制器

//
// WDPersonMessageDetailVC.m
// WestDevelopment
//
// Created by wangtao on 2017/6/23.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageDetailVC.h"
#import "WDPersonMessageDetailCell.h"
#import "WDPersonMessageFooterCell.h"
#import "WDPersonMessageDetailModel.h"

#import 

@interface WDPersonMessageDetailVC ()

@property (nonatomic, weak) WDPersonMessageFooterCell *textfieldView;

@end

@implementation WDPersonMessageDetailVC

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  CGFloat contentOffsetY = scrollView.contentOffset.y;

//  页面下滑,并且输入框还是第一响应的时候,控制器要失去第一响应
  if (contentOffsetY > 10) {
    if (self.textfieldView.isFirst) {
      [self clickSelf];
    }
  }

  //  页面上滑,控制器成为第一响应
  if (contentOffsetY 

输入框 UITableViewHeaderFooterView

//
// WDPersonMessageFooterCell.h
// WestDevelopment
//
// Created by wangtao on 2017/6/26.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDBaseTVHeaderFooterView.h"

typedef void(^ClickSender_t)(NSString *text);

typedef void(^ResignFirstResponder)();

@interface WDPersonMessageFooterCell : WDBaseTVHeaderFooterView

@property (nonatomic, copy) ClickSender_t clickSenderText;
@property (nonatomic, copy) ResignFirstResponder resignFirstRes;

@property (nonatomic, assign) BOOL isFirst;
@property (nonatomic, assign) BOOL sendSucceed;

@end

//
// WDPersonMessageFooterCell.m
// WestDevelopment
//
// Created by wangtao on 2017/6/26.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageFooterCell.h"


@interface WDPersonMessageFooterCell () 

@property (nonatomic, weak) UITextField *textField;
@property (nonatomic, weak) UIView *line;

@end


@implementation WDPersonMessageFooterCell

@synthesize isFirst = _isFirst;

- (void)setupAll
{
  self.contentView.backgroundColor = WTHexColor(0xf2f2f2);

  UITextField *textField = [[UITextField alloc] init];
  textField.backgroundColor = kWhiteColor;
  [self.contentView addSubview:textField];
  textField.delegate = self;
  self.textField = textField;
  textField.layer.cornerRadius = 3;
  textField.layer.masksToBounds = YES;

  textField.returnKeyType = UIReturnKeySend;

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageState:) name:kMessageState object:nil];

  UIView *line = [[UIView alloc] init];
  line.backgroundColor = WTHexColor(0xdddddd);
  [self.contentView addSubview:line];
  self.line = line;

}

- (void)messageState:(NSNotification *)noti
{
  NSInteger state = [[noti object] boolValue];
  if (state) {
    [self.textField resignFirstResponder];
    if (self.resignFirstRes) {
      self.resignFirstRes();
    }
  }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
  if (self.clickSenderText) {
    self.clickSenderText(self.textField.text);
  }
  return YES;
}

- (void)setIsFirst:(BOOL)isFirst
{
  _isFirst = isFirst;
  if (isFirst) {
    [self.textField becomeFirstResponder];
  } else {
    [self.textField resignFirstResponder];
  }
}

- (BOOL)isFirst
{
  if ([self.textField isFirstResponder]) {
    return YES;
  }
  return NO;
}

- (void)setSendSucceed:(BOOL)sendSucceed
{
  self.textField.text = @"";
}

- (void)layoutSubviews
{
  [super layoutSubviews];

  CGFloat padding = 10;
  self.textField.frame = CGRectMake(padding, padding, self.wt_width - padding * 2, self.wt_height - padding * 2);
  self.line.frame = CGRectMake(0, 0, self.wt_width, .5);
}

@end

消息cell

//
// WDPersonMessageDetailCell.m
// WestDevelopment
//
// Created by wangtao on 2017/6/23.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageDetailCell.h"
#import "WDPersonMessageDetailModel.h"


@interface WDPersonMessageDetailCell ()

@property (nonatomic, weak) UILabel *time;
@property (nonatomic, weak) UIImageView *icon;
@property (nonatomic, weak) UILabel *detail;

@property (nonatomic, weak) UIView *baseView;

@end

@implementation WDPersonMessageDetailCell

- (void)setupAll
{
  self.selectionStyle = UITableViewCellSelectionStyleNone;

  self.backgroundColor = WTHexColor(0xeaeaea);
  self.contentView.backgroundColor = WTHexColor(0xeaeaea);

  UILabel *time = [UILabel labelWithText:@""
                 textColor:WTHexColor(0xaaaaaa)
               textAlignment:NSTextAlignmentCenter
                   font:12
              backgroundColor:kClearColor];
  [self.contentView addSubview:time];
  self.time = time;

  UIImageView *icon = [[UIImageView alloc] init];
  [self.contentView addSubview:icon];
  icon.image = [UIImage imageNamed:kDefault];
  self.icon = icon;
  self.icon.layer.cornerRadius = 35 / 2;
  self.icon.layer.masksToBounds = YES;

  UIView *baseView = [[UIView alloc] init];
  [self.contentView addSubview:baseView];
  self.baseView = baseView;
  baseView.layer.masksToBounds = YES;
  baseView.layer.cornerRadius = 4;

  UILabel *detail = [UILabel labelWithText:@""
                  textColor:kBlackColor
                textAlignment:NSTextAlignmentLeft
                    font:13
               backgroundColor:kClearColor];
  [baseView addSubview:detail];
  self.detail = detail;
  detail.numberOfLines = 0;

}

- (void)setModel:(WDPersonMessageDetailModel *)model
{
  _model = model;

  if ([model.isShow isEqualToString:@"1"]) {
    self.time.text = model.addTime;
    self.time.hidden = NO;
    self.time.frame = CGRectMake(0, 0, kMainScreenWidth, 20);

  } else {
    self.time.text = @"";
    self.time.hidden = YES;
    self.time.frame = CGRectZero;

  }

  self.time.text = model.addTime;
  [self.icon wt_setImageWithUrlString:model.headImg placeholderString:@"me_icon"];
  self.detail.text = model.comment;

  if ([model.userId isEqualToString:[WTAccount shareAccount].uid]) {
    self.detail.textColor = kBlackColor;
    self.baseView.backgroundColor = kWhiteColor;

    self.icon.frame = CGRectMake(kPadding, self.time.wt_bottom + kPadding, 35, 35);
    self.baseView.frame = CGRectMake(self.icon.wt_right + kPadding, self.icon.wt_top, model.commentW, model.commentH);
    self.detail.frame = CGRectMake(kPadding, kPadding, model.commentW - kPadding * 2, model.commentH - kPadding * 2);

  } else {
    self.detail.textColor = kWhiteColor;
    self.baseView.backgroundColor = kHomeColor;

    self.icon.frame = CGRectMake(kMainScreenWidth - 35 - kPadding, self.time.wt_bottom + kPadding, 35, 35);
    self.baseView.frame = CGRectMake(self.icon.wt_left - kPadding - model.commentW, self.icon.wt_top, model.commentW, model.commentH);
    self.detail.frame = CGRectMake(kPadding, kPadding, model.commentW - kPadding * 2, model.commentH - kPadding * 2);

  }

}


@end

模型

//
// WDPersonMessageDetailModel.m
// WestDevelopment
//
// Created by wangtao on 2017/6/23.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageDetailModel.h"

@implementation WDPersonMessageDetailModel

- (CGFloat)commentW
{
  if (_commentW == 0) {
    _commentW = [self.comment wt_calculateStringSizeWithFontOfSize:13 maxWidth:kMainScreenWidth / 2].width + 20;
  }
  return _commentW;
}

- (CGFloat)commentH
{
  if (_commentH == 0) {
    CGFloat textH = [self.comment wt_calculateStringSizeWithFontOfSize:13 maxWidth:kMainScreenWidth / 2].height;
    // 一行字体是15高,一行的情况就和头像一样高
    _commentH = (textH 

 

本文是爱站技术频道小编带给大家的IOS开发中构建聊天页面的情况,其实也是非常简单的,就算没有任何的经验,应该也可以独立完成。

 

上一篇:IOS开发中启动页面倒计时的操作方法

下一篇:IOS开发之在进入后台后的重置方法

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载