IOS开发中表情键盘代码获取

来源:爱站网时间:2020-12-24编辑:网友分享
开发的时候通常都会模拟会话在其他应用程序中操作,但是并不是很多功能都能进行模拟,大家赶紧跟着爱站技术频道小编来一起看看IOS开发中表情键盘代码获取,感兴趣的小伙伴们一起来看看吧!

开发的时候通常都会模拟会话在其他应用程序中操作,但是并不是很多功能都能进行模拟,大家赶紧跟着爱站技术频道小编来一起看看IOS开发中表情键盘代码获取,感兴趣的小伙伴们一起来看看吧!
1.用到的表情字符串是从Emojiplist文件里获取到的;

2.需要添加一个观察者:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
 
- (void)keyboardWillShow:(NSNotification *)notification
{
  // 键盘显示\隐藏完毕的frame
  CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  // 动画时间
  CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
   
  // 动画
  [UIView animateWithDuration:duration animations:^{
    commentView.minY = -frame.size.height;
  }];
}

3.创建控件:

  //声明的全局变量:
  UIButton *commentView;
  UIView *commentWhiteColorView;
  UITextField *commentTextField;
  UIButton *emojiAndKeyboardButton;
 
- (void)initCommentToolbarView
{
  commentView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight + 230)];
  commentView.hidden = YES;
  [commentView addTarget:self action:@selector(commentViewAction) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:commentView];
   
  commentWhiteColorView = [UIView viewWithFrame:CGRectMake(0, kScreenHeight - 50, kScreenWidth, 50) backgroundColor:[UIColor whiteColor]];
  commentWhiteColorView.backgroundColor = [UIColor whiteColor];
  [commentView addSubview:commentWhiteColorView];
   
  UIView *lightGrayLineView = [UIView viewWithFrame:CGRectMake(0, 0, kScreenWidth, 1) backgroundColor:RGB(240, 240, 240)];
  [commentWhiteColorView addSubview:lightGrayLineView];
   
  //文本输入框
  commentTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 5, kScreenWidth - (10 + 42 + 60), 40)];
  commentTextField.font = FONT(14);
  commentTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
  commentTextField.leftViewMode = UITextFieldViewModeAlways;
  commentTextField.backgroundColor = RGB(234, 234, 234);
  commentTextField.placeholder = @"评论";
  [commentWhiteColorView addSubview:commentTextField];
   
  //表情和键盘切换按钮
  emojiAndKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];
  emojiAndKeyboardButton.frame = CGRectMake(commentTextField.maxX + 7, 0, 35, 50);
  [emojiAndKeyboardButton setImage:[UIImage imageNamed:@"icon_emoji_input"] forState:UIControlStateNormal];
  [emojiAndKeyboardButton setImage:[UIImage imageNamed:@"icon_keyboard_input"] forState:UIControlStateSelected];
  [emojiAndKeyboardButton addTarget:self action:@selector(emojiAndKeyboardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  [commentWhiteColorView addSubview:emojiAndKeyboardButton];
   
  //发送按钮
  UIButton *sendButton = [UIButton buttonWithFrame:CGRectMake(emojiAndKeyboardButton.maxX, commentTextField.minY, 50, 40) type:UIButtonTypeCustom title:@"发送" titleColor:RGB(135, 135, 135) imageName:nil action:@selector(sendButtonAction) target:self];
  sendButton.titleLabel.font = FONT(14);
  [sendButton setBorder:1 color:RGB(135, 135, 135)];
  [sendButton setCornerRadius:3];
  [commentWhiteColorView addSubview:sendButton];
   
  //表情滚动视图
  emojiScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, commentWhiteColorView.maxY, kScreenWidth, 200)];
  emojiScrollView.backgroundColor = RGB(244, 244, 246);
  emojiScrollView.delegate = self;
  emojiScrollView.pagingEnabled = YES;
  [commentView addSubview:emojiScrollView];
   
  //从文件里获取到的表情字符串数组
  emojiArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Emoji" ofType:@"plist"]];
   
  CGFloat emojiButtonWidth = kScreenWidth/8;
   
  int i = 0;
  //页数向上取整
  int page = ceilf(emojiArray.count/32.0);
   
  //UIKit里的页面控制器
  pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, emojiScrollView.maxY, kScreenWidth, 30)];
  pageControl.numberOfPages = page;
  pageControl.backgroundColor = RGB(244, 244, 246);
  pageControl.pageIndicatorTintColor = RGB(206, 206, 206);
  pageControl.currentPageIndicatorTintColor = RGB(121, 121, 121);
  [commentView addSubview:pageControl];
   
  //设置表情滚动视图的contentSize
  emojiScrollView.contentSize = CGSizeMake(kScreenWidth * page, 200);
  //循环创建表情按钮
  for (int currentPage = 0; currentPage  1) {
    //判断是否是表情,表情length为2,所以减去2
    if ([emojiArray containsObject:[commentTextField.text substringWithRange:NSMakeRange(commentTextField.text.length - 2, 2)]]) {
      commentTextField.text = [commentTextField.text substringToIndex:commentTextField.text.length - 2];
    }else{
      commentTextField.text = [commentTextField.text substringToIndex:commentTextField.text.length - 1];
    }
  }else{
    commentTextField.text = @"";
  }
}
 
//在代理方法中调整pageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  if (scrollView == emojiScrollView) {
    pageControl.currentPage = scrollView.contentOffset.x/scrollView.width;
  }
}
 
//表情和键盘切换按钮事件
- (void)emojiAndKeyboardButtonAction:(UIButton *)sender
{
  sender.selected = !sender.selected;
   
  if (sender.selected == YES) {
    [commentTextField resignFirstResponder];
     
    [UIView animateWithDuration:0.5 animations:^{
      commentView.minY = -230;
    }];
  }else{
    [commentTextField becomeFirstResponder];
  }
}
 
- (void)commentViewAction
{
  [commentTextField resignFirstResponder];
   
  commentView.hidden = YES;
  commentView.minY = 0;
  commentTextField.text = @"";
  emojiAndKeyboardButton.selected = NO;
}

以上就是爱站技术频道小编给大家介绍的IOS开发中表情键盘代码获取,各位程序员千万不要急躁,关注js.aizhan.com了解更多的专业知识。

上一篇:IOS开发中异步网络请求同步的操作详解

下一篇:IOS开发中自定义uiscrollview的滚动条的实现步骤

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载