返回
Swift自定义表情键盘 + 录音
IOS
2024-01-19 12:04:27
绪论
如果你厌倦了使用iPhone自带的无聊表情,那么是时候创建你自己的表情了。借助Swift,你可以轻松地创建表情键盘,并将其添加到你的iPhone。
创建自定义表情键盘
- 打开Xcode并创建一个新的iOS项目。
- 选择"Single View App"模板并点击"Next"。
- 为你的项目输入一个名称并选择一个保存位置。
- 点击"Create"按钮以创建你的项目。
- 在项目导航器中,选择你的项目文件。
- 在"Build Settings"选项卡中,找到"Product Bundle Identifier"设置。
- 将"Product Bundle Identifier"的值更改为你自己的包标识符。
- 在项目导航器中,选择"main.storyboard"文件。
- 在工具栏中,点击"Assistant Editor"按钮。
- 在"Assistant Editor"中,选择"View Controller"场景。
- 在"View Controller"场景中,拖动"Button"控件到视图。
- 在"Button"控件的"Title"属性中,输入"Create Emoji Keyboard"。
- 在"Button"控件的"Action"属性中,选择"touchUpInside"。
- 在"touchUpInside"方法中,添加以下代码:
let emojiKeyboard = EmojiKeyboard()
emojiKeyboard.delegate = self
self.view.addSubview(emojiKeyboard)
- 在项目导航器中,选择"EmojiKeyboard.swift"文件。
- 在"EmojiKeyboard.swift"文件中,添加以下代码:
import UIKit
class EmojiKeyboard: UIView {
var delegate: EmojiKeyboardDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
private func setup() {
self.backgroundColor = UIColor.white
self.frame = CGRect(x: 0, y: 0, width: self.superview!.frame.width, height: 250)
self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let emojiButtons = [
"😀", "😁", "😆", "😅", "😂", "🤣", "🥲", "🥹", "😋", "😛",
"😜", "🤪", "😝", "🤑", "🤗", "🤭", "🤫", "🤔", "😐", "😑",
"😶", "🙄", "😏", "😒", "😬", "🤥"
]
var x: CGFloat = 0
var y: CGFloat = 0
for emoji in emojiButtons {
let button = UIButton(frame: CGRect(x: x, y: y, width: 50, height: 50))
button.setTitle(emoji, for: .normal)
button.addTarget(self, action: #selector(emojiButtonPressed), for: .touchUpInside)
self.addSubview(button)
x += 50
if x > self.frame.width - 50 {
x = 0
y += 50
}
}
}
@objc private func emojiButtonPressed(_ sender: UIButton) {
self.delegate?.emojiKeyboard(self, didSelectEmoji: sender.titleLabel!.text!)
}
}
protocol EmojiKeyboardDelegate {
func emojiKeyboard(_ emojiKeyboard: EmojiKeyboard, didSelectEmoji emoji: String)
}
- 在项目导航器中,选择"ViewController.swift"文件。
- 在"ViewController.swift"文件中,添加以下代码:
import UIKit
class ViewController: UIViewController, EmojiKeyboardDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func emojiKeyboard(_ emojiKeyboard: EmojiKeyboard, didSelectEmoji emoji: String) {
print("Selected emoji: \(emoji)")
}
}
- 构建并运行你的项目。
- 当你点击"Create Emoji Keyboard"按钮时,自定义表情键盘就会出现在屏幕上。
将录音功能集成到自定义的表情键盘
- 在项目导航器中,选择"EmojiKeyboard.swift"文件。
- 在"EmojiKeyboard.swift"文件中,添加以下代码:
import AVFoundation
class EmojiKeyboard: UIView, AVAudioRecorderDelegate {
// ...
private var audioRecorder: AVAudioRecorder?
private func startRecording() {
let settings = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2
]
audioRecorder = try? AVAudioRecorder(url: FileManager.default.temporaryDirectory.appendingPathComponent("recording.m4a"), settings: settings)
audioRecorder?.delegate = self
audioRecorder?.record()
}
private func stopRecording() {
audioRecorder?.stop()
}
// ...
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
if flag {
self.delegate?.emojiKeyboard(self, didRecordAudio: recorder.url)
}
}
}
protocol EmojiKeyboardDelegate {
// ...
func emojiKeyboard(_ emojiKeyboard: EmojiKeyboard, didRecordAudio url: URL)
}
- 在项目导航器中,选择"ViewController.swift"文件。
- 在"ViewController.swift"文件中,添加以下代码:
import UIKit
import AVFoundation
class ViewController: UIViewController, EmojiKeyboardDelegate {
// ...
func emojiKeyboard(_ emojiKeyboard: EmojiKeyboard, didRecordAudio url: URL) {
// Do something with the recorded audio
}
}
- 构建并运行你的项目。
- 当你点击自定义表情键盘上的录音按钮时,录音功能就会启动。
- 当你再次点击录音按钮时,录音功能就会停止。
结论
我希望这篇博客能帮助你创建自己的自定义表情键盘,并将其集成到你的iPhone。如果你有任何问题,请随时留言。