Ios 将自定义键盘嵌入uiview
我一直在研究如何将自己的自定义十进制键盘嵌入UIView。我发现下面的图片,我想重新创建。然而,我不确定如何以及什么是开始这样做的最佳方式?只是创建多个UIButton,然后用一种方法处理它,还是有一种聪明的方法来重新创建这样的UIButtonIos 将自定义键盘嵌入uiview,ios,swift,uiview,Ios,Swift,Uiview,我一直在研究如何将自己的自定义十进制键盘嵌入UIView。我发现下面的图片,我想重新创建。然而,我不确定如何以及什么是开始这样做的最佳方式?只是创建多个UIButton,然后用一种方法处理它,还是有一种聪明的方法来重新创建这样的UIButton 创建单个UIButton操作出口,将所有按钮连接到它,检查发件人标题值,将其转换为Int,如果不可能,则为逗号,如果标题文本为空,则为退格。或者为退格创建单独的函数。这将是一种相对简单、无杂乱的方法,只需要十几行代码 collectionView是一种很
创建单个UIButton操作出口,将所有按钮连接到它,检查发件人标题值,将其转换为Int,如果不可能,则为逗号,如果标题文本为空,则为退格。或者为退格创建单独的函数。这将是一种相对简单、无杂乱的方法,只需要十几行代码 collectionView是一种很好的方法。创建一个新的故事板,在其中放置一个UICollectionViewController。然后创建一个UICollectionViewCell,其中数字为UILabel,删除按钮为点和UIImage 以下是我的UICollectionViewController代码:
import UIKit
protocol KeypadDelegate: class {
func did(tapOnKeypad key: KeypadKey)
}
enum KeypadKey {
case number (value: Int)
case backspace
}
class KeypadViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
// MARK: - Properties.
weak var delegate: KeypadDelegate?
// MARK: - Lifecycle.
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(cellReuseID: KeypadCVCell.reuseID)
}
// MARK: - UICollectionView DataSource.
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "KeypadCVCell", for: indexPath) as! KeypadCVCell
switch indexPath.row {
case 0...8:
cell.configure(number: String(indexPath.row + 1))
case 9:
cell.setBlank()
case 10:
cell.configure(number: "0")
case 11:
let image = UIImage(named: "btn_keypad_backspace")
cell.configure(image: image)
default:
break
}
return cell
}
// MARK: - UICollectionView Delegate.
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch indexPath.row {
case 0...8:
let key: KeypadKey = .number(value: indexPath.row + 1)
delegate?.did(tapOnKeypad: key)
case 9:
break
case 10:
let key: KeypadKey = .number(value: 0)
delegate?.did(tapOnKeypad: key)
case 11:
delegate?.did(tapOnKeypad: .backspace)
default:
break
}
}
// MARK: - UICollectionView Delegate FlowLayout.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width / 3
let height = collectionView.bounds.height / 4
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
class KeypadCVCell: UICollectionViewCell {
// MARK: - Outlets.
@IBOutlet weak var numberLabel: UILabel!
@IBOutlet weak var backspaceImageView: UIImageView!
// MARK: - Configuration.
func configure(number: String) {
self.numberLabel.text = number
self.backspaceImageView.isHidden = true
}
func configure(image: UIImage?) {
self.numberLabel.isHidden = true
self.backspaceImageView.image = image
}
func setBlank() {
self.numberLabel.isHidden = true
self.backspaceImageView.isHidden = true
}
}
很好,你也可以发布键盘CVCell:-只是为了理解添加到我帖子中的CellForItem。