如何使用UICollectionView里面的几个按钮

时间:2018-03-25 17:26:56

标签: ios swift uicollectionview

我有这个宠物项目里面有一堆按钮,每个按钮都有一个touchDown上的动作,它的工作没有问题。但问题是当我有10x10或更多并且我拖动UICollectionView时,这样的按钮不能再被触摸了!他们不会回应任何事件,我也无法意识到发生了什么。

查看截图 enter image description here

这个游戏必须有几个单元,它可能很大,但我不认为100x100对于任何现代的IOS手机来说都不够大,它没有超过60mb内存使用量,所以我不认为它的问题。 看看我的一些代码片段。

我的视图控制器

@IBOutlet weak var myView: UICollectionView!

let squareButton: SquareButton = buttonSquare(row: row, col: col, frameWidth: self.myView.frame.width, worldSize: worldSize).squareButton
                    squareButton.addTarget(self, action: #selector(ViewController.squareButtonPressed(_:)), for: .touchUpInside)
                    squareButton.tag = buttonCount
                    self.myView.addSubview(squareButton)

构建这些按钮的结构

struct buttonSquare {
        var row: Int
        var col: Int
        var frameWidth: CGFloat
        var worldSize: Int
        var squareButton: SquareButton {
            get {
                let square = Square(row: row, col: col)
                let squareSize:CGFloat =  frameWidth / 20.0 //CGFloat(worldSize)
                let squareButton = SquareButton(squareModel: square, squareSize: squareSize, squareMargin1: 1.0);
                squareButton.setTitleColor(UIColor.darkGray, for: .normal)
                // squareButton.addTarget(self, action: #selector(ViewController.squareButtonPressed(_:)), for: .touchDown)
                return squareButton
            }
        }
    }

我的按钮代码

import Foundation
import UIKit

class SquareButton : UIButton {

    var squareSize:CGFloat
    var squareMargin:CGFloat
    var square:Square

    init(squareModel:Square, squareSize:CGFloat, squareMargin1:CGFloat) {
        self.square = squareModel
        self.squareSize = squareSize
        self.squareMargin = squareMargin1
        let x = CGFloat(self.square.col) * (squareSize + squareMargin1)
        let y = CGFloat(self.square.row) * (squareSize + squareMargin1)
        let squareFrame = CGRect(x:x, y:y, width:squareSize, height:squareSize)
        super.init(frame: squareFrame)
        self.backgroundColor = squareModel.live ? .black : .white
        self.layer.borderWidth = 0.8
        self.layer.borderColor = UIColor.black.cgColor
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
    }

    func change(_ state:Bool) {
        self.backgroundColor = state ? .black : .white
        square.live = state
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

I can't click on buttons beyond that position

Xcode 9,迅捷4 谢谢。

0 个答案:

没有答案