将自定义键盘嵌入uiview

时间:2017-12-17 14:55:31

标签: ios swift uiview

我一直在研究如何将自己的自定义小数键盘嵌入到UIView中。我发现下面的图像,我想重新创建。但是,我不确定如何以及如何开始这样的最佳方式?是创建多个UIButton然后在方法中处理它还是有一种聪明的方法来重新创建它?

enter image description here

2 个答案:

答案 0 :(得分:1)

collectionView是一种很好的方法。创建一个新的故事板,在其中放置一个UICollectionViewController。然后使用UILabel(用于数字和点)和UIImage(用于删除按钮)创建UICollectionViewCell。

这是我的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
    }

}

答案 1 :(得分:0)

创建单个UIButton操作插座,将所有按钮连接到它,检查发件人标题值,将其转换为Int,如果不可能,则为逗号,或者如果标题文本为空,则为退格。或者创建退格的单独功能。这将是一种相对简单,无杂乱的方式,并且不会花费十几行代码。