在 swift 4 或更高版本上创建触摸测试应用

create touch test apps on swift 4 or above

美好的一天,我想制作一个应用来检测 ios 设备 (iphone) 屏幕上的触摸。但我是 swift

的新人

我预计,当打开应用程序时,应用程序的界面就像我附上的图像一样,如果我触摸小方块,它会从界面上消失,如果我触摸了所有小方块,它们会一个一个消失。如果所有小方块都消失了,应用程序将显示 UIAlert 成功并退出。请帮助我,我需要你的指导。谢谢你 Imgur link 我附上的图片

您可以使用 collectionview 创建布局。使用一些颜色作为默认的 collectionview 单元格颜色。当一个集合视图单元格被选中时,你将颜色更改为清除颜色。选择所有单元格后显示警报。

class ViewControllerNew: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    let collectionView = CollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    var arr = [Int]()
    var deletedArr = [Int]()
    var rowCount = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.register(Cell1.self, forCellWithReuseIdentifier: "Cell1")
        collectionView.isScrollEnabled = false
        view.addSubview(collectionView)

        collectionView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
        collectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        collectionView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true

        if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            let itemSpacing: CGFloat = 5
            let itemsInOneRow: CGFloat = 5
            layout.sectionInset = UIEdgeInsets(top: itemSpacing, left: itemSpacing, bottom: itemSpacing, right: itemSpacing)
            layout.minimumInteritemSpacing = itemSpacing
            layout.minimumLineSpacing = itemSpacing
            let cellWidth = (UIScreen.main.bounds.width - (itemSpacing * 2) - ((itemsInOneRow - 1) * itemSpacing)) / itemsInOneRow
            let rowCount = UIScreen.main.bounds.height / cellWidth
            let newRowCount = Int((UIScreen.main.bounds.height - (itemSpacing * 2) - ((rowCount - 1) * itemSpacing)) / cellWidth)
            layout.itemSize = CGSize(width: cellWidth, height: cellWidth)
            self.arr = Array(0..<newRowCount*Int(itemsInOneRow))
            collectionView.reloadData()
        }
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arr.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath)
        cell.backgroundColor = .red
        cell.isUserInteractionEnabled = true
        return cell
    }
    func updateCells(_ touches: Set<UITouch>) {
        guard let location = touches.first?.location(in: collectionView),
                let indexPath = collectionView.indexPathForItem(at: location),
                let cell = collectionView.cellForItem(at: indexPath) else {
                    return
        }
        cell.backgroundColor = .clear
        if !deletedArr.contains(indexPath.item) {
            deletedArr.append(indexPath.item)
        }
        if deletedArr.count == arr.count {
            let alert = UIAlertController(title: "Game Finished", message: nil, preferredStyle: .alert)
            let okBtn = UIAlertAction(title: "Ok", style: .default) { action in
                self.deletedArr.removeAll()
                self.collectionView.reloadData()
            }
            alert.addAction(okBtn)
            self.present(alert, animated: true, completion: nil)
        }
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        updateCells(touches)
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        updateCells(touches)
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        updateCells(touches)
    }
}
class Cell1: UICollectionViewCell {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        return nil
    }
}
class CollectionView: UICollectionView {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        return nil
    }
}