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

标签 swift touch detect

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

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

最佳答案

您可以使用collectionview用于创建布局。使用某种颜色作为默认的 Collection View 单元格颜色。当选择 Collection View 单元格时,您可以将颜色更改为透明颜色。当选择所有单元格时,会出现警报。

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
    }
}

enter image description here

关于swift - 在 swift 4 或更高版本上创建触摸测试应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56271567/

相关文章:

iphone - iPhone SDK 检测耳机按钮点击

swift - 如何在 iphone 中实现蓝牙 pan 配置文件

json - Swift 3 - 将模拟的 JSON 声明为来自服务器的响应

ios - Swift - iOS - 引用字符串文字时找不到 XCode 6.1 dyld fatal error 符号

ios - Swift 中的线程队列

android - 如何检查我是否触摸了模态 Activity 的内部或外部?

javascript - 使用 WinJs 构建的 Pivot 的滑动事件在 Windows Phone 上不起作用

c++ - 函数如何检测字符串指针与字符串文字参数?

javascript - 在触摸绑定(bind)元素内单击被忽略

php - 检测字符串是否包含任何数字