iOS表情鍵盤(pán)的簡(jiǎn)單實(shí)現(xiàn)代碼
最近用到了表情鍵盤(pán)就去網(wǎng)上找了下,感覺(jué)網(wǎng)上的都是為了更大的需求寫(xiě)的,而我并不需要所以就自己寫(xiě)了個(gè)簡(jiǎn)單的實(shí)現(xiàn)。
1.用到的表情字符串是從Emojiplist文件里獲取到的;
2.需要添加一個(gè)觀察者:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; - (void)keyboardWillShow:(NSNotification *)notification { // 鍵盤(pán)顯示\隱藏完畢的frame CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 動(dòng)畫(huà)時(shí)間 CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 動(dòng)畫(huà) [UIView animateWithDuration:duration animations:^{ commentView.minY = -frame.size.height; }]; }
3.創(chuàng)建控件:
//聲明的全局變量: 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 = @"評(píng)論"; [commentWhiteColorView addSubview:commentTextField]; //表情和鍵盤(pán)切換按鈕 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]; //發(fā)送按鈕 UIButton *sendButton = [UIButton buttonWithFrame:CGRectMake(emojiAndKeyboardButton.maxX, commentTextField.minY, 50, 40) type:UIButtonTypeCustom title:@"發(fā)送" 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]; //表情滾動(dòng)視圖 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]; //從文件里獲取到的表情字符串?dāng)?shù)組 emojiArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Emoji" ofType:@"plist"]]; CGFloat emojiButtonWidth = kScreenWidth/8; int i = 0; //頁(yè)數(shù)向上取整 int page = ceilf(emojiArray.count/32.0); //UIKit里的頁(yè)面控制器 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]; //設(shè)置表情滾動(dòng)視圖的contentSize emojiScrollView.contentSize = CGSizeMake(kScreenWidth * page, 200); //循環(huán)創(chuàng)建表情按鈕 for (int currentPage = 0; currentPage < page; currentPage++) { for (int row = 0; row < 4; row++) { for (int column = 0; column < 8; column++) { UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom]; if (row == 3 && column == 7) { //如果是第4行第8列就設(shè)置刪除表情的圖片替代字符串,并調(diào)用另一個(gè)方法 [emojiButton setImage:[UIImage imageNamed:@"back_icon_input"] forState:UIControlStateNormal]; [emojiButton addTarget:self action:@selector(deleteEmojiAction) forControlEvents:UIControlEventTouchUpInside]; }else{ [emojiButton setTitle:emojiArray[i++] forState:UIControlStateNormal]; [emojiButton addTarget:self action:@selector(emojiButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } emojiButton.frame = CGRectMake(emojiButtonWidth * column + currentPage * kScreenWidth, 50 * row, emojiButtonWidth, 50); [emojiScrollView addSubview:emojiButton]; //當(dāng)i等于數(shù)組計(jì)數(shù)時(shí)就打斷循環(huán) if (i == emojiArray.count) { break; } } } } //手動(dòng)添加最后一個(gè)刪除表情按鈕 UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom]; [emojiButton setImage:[UIImage imageNamed:@"back_icon_input"] forState:UIControlStateNormal]; emojiButton.frame = CGRectMake(emojiButtonWidth * 7 + 5 * kScreenWidth, 50 * 3, emojiButtonWidth, 50); [emojiButton addTarget:self action:@selector(deleteEmojiAction) forControlEvents:UIControlEventTouchUpInside]; [emojiScrollView addSubview:emojiButton]; } //表情按鈕事件 - (void)emojiButtonAction:(UIButton *)sender { // NSLog(@"%@",sender.currentTitle); NSMutableString *oldText = [NSMutableString stringWithString:commentTextField.text]; [oldText appendString:sender.currentTitle]; commentTextField.text = oldText; } //刪除表情按鈕事件 - (void)deleteEmojiAction { if (commentTextField.text.length > 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 = @""; } } //在代理方法中調(diào)整pageControl - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView == emojiScrollView) { pageControl.currentPage = scrollView.contentOffset.x/scrollView.width; } } //表情和鍵盤(pán)切換按鈕事件 - (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; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS8調(diào)用相機(jī)報(bào)警告Snapshotting a view的解決方法
這篇文章主要介紹了iOS8調(diào)用相機(jī)報(bào)警告Snapshotting a view……的解決方法 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11React Native學(xué)習(xí)教程之自定義NavigationBar詳解
這篇文章主要給大家介紹了關(guān)于React Native學(xué)習(xí)教程之自定義NavigationBar的相關(guān)資料,文中通過(guò)是示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10ios swift3.0實(shí)現(xiàn)二維碼掃描、生成、識(shí)別示例代碼
本篇文章主要介紹了ios swift3.0實(shí)現(xiàn)二維碼掃描、生成、識(shí)別示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02iOS如何開(kāi)發(fā)簡(jiǎn)單的手繪應(yīng)用實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于iOS如何開(kāi)發(fā)簡(jiǎn)單的手繪應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09IOS 貝塞爾曲線(UIBezierPath)屬性、方法整理
這篇文章主要介紹了IOS 貝塞爾曲線(UIBezierPath)屬性、方法的相關(guān)資料,這里整理了貝塞爾 曲線的基礎(chǔ)資料,對(duì)屬性及相應(yīng)的方法一一做了詳解,需要的朋友可以參考下2016-11-11iOS App設(shè)計(jì)模式開(kāi)發(fā)中對(duì)迭代器模式的使用示例
這篇文章主要介紹了iOS App設(shè)計(jì)模式開(kāi)發(fā)中對(duì)迭代器模式的使用示例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03IOS開(kāi)發(fā)之路--C語(yǔ)言存儲(chǔ)方式和作用域
只有你完全了解每個(gè)變量或函數(shù)存儲(chǔ)方式、作用范圍和銷(xiāo)毀時(shí)間才可能正確的使用這門(mén)語(yǔ)言。今天將著重介紹C語(yǔ)言中變量作用范圍、存儲(chǔ)方式、生命周期、作用域和可訪問(wèn)性。2014-08-08iOS獲取當(dāng)前設(shè)備型號(hào)等信息(全)包含iPhone7和iPhone7P
這篇文章主要介紹了iOS獲取當(dāng)前設(shè)備型號(hào)設(shè)備信息的總結(jié)包含iPhone7和iPhone7P,包括ios7之前之后的獲取方式,本文接的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10