聊天表情键盘实现

先把目前完成的整理出来,目前个人思路还比较乱,后续优化

image

思路

页面底部使用UIView
UIView中包含 textview 、button 、collectionView
添加 NSNotificationCenter 监听键盘 移动view
语音button 控制 textview 及 键盘
其他button 控制 键盘 及 collectionView

使用键盘

1
2
// 键盘
let v:UIView = InputBar(frame: CGRectMake(0,h-50,w,50))

键盘定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import UIKit

class InputBar: UIView , UITextViewDelegate {

var textview:UITextView!//文本
var voicebtn:UIButton!//话筒
var facebtn:UIButton!//表情
var morebtn:UIButton!//更多

override init(frame: CGRect) {
super.init(frame: frame)

let w:CGFloat = frame.size.width
let h:CGFloat = frame.size.height

self.backgroundColor = UIColor(red: 0.922, green: 0.925, blue: 0.929, alpha: 1)

//初始化键盘组件
var x:CGFloat = 0
voicebtn = UIButton(frame: CGRectMake(0,0,h,h))
voicebtn.addTarget(self, action: "voicebtnclick:", forControlEvents: UIControlEvents.TouchUpInside)
voicebtn.setImage(UIImage(named: "chat_bottom_voice_nor"), forState: UIControlState.Normal)
voicebtn.setImage(UIImage(named: "chat_bottom_voice_press"), forState: UIControlState.Highlighted)
voicebtn.setImage(UIImage(named: "chat_bottom_keyboard_nor"), forState: UIControlState.Selected)
self.addSubview(voicebtn)

x += voicebtn.frame.size.width
textview = UITextView(frame: CGRectMake(x, (h-35)/2, w-(3*h), 35))
textview.delegate = self
textview.layer.masksToBounds = true
textview.layer.cornerRadius = 4
textview.layer.borderWidth=1
textview.layer.borderColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.4).CGColor
textview.scrollIndicatorInsets = UIEdgeInsetsMake(10, 0, 10, 4)
textview.contentInset = UIEdgeInsetsZero
textview.scrollEnabled = false
textview.scrollsToTop = false
textview.userInteractionEnabled = true
textview.font = UIFont.systemFontOfSize(14)
textview.textColor = UIColor.blackColor()
textview.keyboardAppearance = UIKeyboardAppearance.Default
textview.keyboardType = UIKeyboardType.Default
textview.returnKeyType = UIReturnKeyType.Send
textview.textAlignment = NSTextAlignment.Left
self.addSubview(textview)

x += textview.frame.size.width
facebtn = UIButton(frame: CGRectMake(x,0,h,h))
facebtn.addTarget(self, action: "facebtnclick:", forControlEvents: UIControlEvents.TouchUpInside)
facebtn.setImage(UIImage(named: "chat_bottom_smile_nor"), forState: UIControlState.Normal)
facebtn.setImage(UIImage(named: "chat_bottom_smile_press"), forState: UIControlState.Highlighted)
facebtn.setImage(UIImage(named: "chat_bottom_keyboard_nor"), forState: UIControlState.Selected)
self.addSubview(facebtn)

x += facebtn.frame.size.width
morebtn = UIButton(frame: CGRectMake(x,0,h,h))
morebtn.addTarget(self, action: "morebtnclick:", forControlEvents: UIControlEvents.TouchUpInside)
morebtn.setImage(UIImage(named: "chat_bottom_up_nor"), forState: UIControlState.Normal)
morebtn.setImage(UIImage(named: "chat_bottom_up_press"), forState: UIControlState.Highlighted)
morebtn.setImage(UIImage(named: "chat_bottom_keyboard_nor"), forState: UIControlState.Selected)
self.addSubview(morebtn)

//监听键盘显示、隐藏变化,让自己伴随键盘移动
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardchange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

deinit{
NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardchange(sender:NSNotification){
let userinfo:NSDictionary = sender.userInfo!

let endframe:CGRect = userinfo.objectForKey(UIKeyboardFrameEndUserInfoKey)!.CGRectValue
let duration:NSTimeInterval = userinfo.objectForKey(UIKeyboardAnimationDurationUserInfoKey) as! NSTimeInterval

UIView.animateWithDuration(duration, delay: 0, options: ( UIViewAnimationOptions.BeginFromCurrentState) , animations: { () -> Void in

var supframe:CGRect = self.superview!.frame

var newframe:CGRect = self.frame
// 输入框y = 键盘y - 输入框高 - navigationbar高
newframe.origin.y = endframe.origin.y - self.bounds.size.height - supframe.origin.y
self.frame = newframe

supframe.size.height = endframe.origin.y

}) { (bol) -> Void in
}
}
func voicebtnclick(sender:AnyObject?){
textview.resignFirstResponder()
let btn = sender as! UIButton
btn.selected = !btn.selected
if btn.selected == false{
textview.becomeFirstResponder()
}else{
facebtn.selected = false
morebtn.selected = false
}
}
func facebtnclick(sender:AnyObject?){
textview.resignFirstResponder()
let btn = sender as! UIButton
btn.selected = !btn.selected
if btn.selected == false{
textview.becomeFirstResponder()
}else{
voicebtn.selected = false
morebtn.selected = false
}
}
func morebtnclick(sender:AnyObject?){
textview.resignFirstResponder()
let btn = sender as! UIButton
btn.selected = !btn.selected
if btn.selected == false{
textview.becomeFirstResponder()
}else{
facebtn.selected = false
voicebtn.selected = false
}
}
}

本文标题:聊天表情键盘实现

文章作者:史彦超

发布时间:2016年10月11日 - 22:10

最后更新:2021年07月20日 - 16:07

原始链接:https://doingself.github.io/2016/10/11/2016-10-11-%E8%81%8A%E5%A4%A9%E8%A1%A8%E6%83%85%E9%94%AE%E7%9B%98%E5%AE%9E%E7%8E%B0/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Donate comment here