我正在制作类似于游戏“ Set”的iOS应用/游戏。
为此,我需要制作多个视图,所以我做了这个课:
class CardSubview: UIView {
private let partsOfSpace:CGFloat = 12
private let occurenceOfForms: CGFloat = 3
private let color1:UIColor = someColor1
private let color2:UIColor = someColor2
private let color3:UIColor = somecolor3
private let attributeIdentifiers = [1,2,3]
private var openCardUp = false
public var isSelceted = false {
didSet {
if isSelceted == true {
self.backgroundColor = #colorLiteral(red: 0.5791940689, green: 0.1280144453, blue: 0.5726861358, alpha: 0.52734375)
self.layer.borderWidth = 5.0
self.layer.borderColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)
}
else {
self.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
self.layer.cornerRadius = 0
self.layer.borderWidth = 0
}
}
}
public func makePath() {
openCardUp = true
let path = coloreAndFill(path: self.occurenceOfForm(form: **index1**, occurence: **index2**, viewWidth: self.bounds.width, viewHeigth: self.bounds.height), chosenColor:**index3**, fillIdentifier: **index4**)
path.stroke()
}
override func draw(_ rect: CGRect) {
if openCardUp == true {
makePath()
}
}
....
因此,所有您需要知道的是,这将使视图具有矩形,三角形或圆形等。
现在我想将80种不同的颜色放入CardBoardView中
这是我的CardBoardView
import UIKit
@IBDesignable
class CardBoardView: UIView {
static var index1 = 1
static var index2 = 1
static var index3 = 1
static var index4 = 3
public var cells = 12
let values = Values()
var card: CardSubview = CardSubview() {
didSet {
let tabRecognizer = UITapGestureRecognizer(target: self, action: #selector(tab))
card.addGestureRecognizer(tabRecognizer)
}
}
struct Values {
public let ratio:CGFloat = 2.0
public let insetByX:CGFloat = 3.0
public let insetByY:CGFloat = 3.0
public let allAvailableCards = 81
}
@objc private func tab (recognizer: UITapGestureRecognizer) {
switch recognizer.state {
case .ended:
card.isSelceted = !card.isSelceted
default: break
}
}
override func layoutSubviews() {
super.layoutSubviews()
var grid = Grid(layout: .aspectRatio(values.ratio), frame: self.bounds)
grid.cellCount = 12
for index in 0..<12 {
card = CardSubview(frame: grid[index]!.insetBy(dx: values.insetByX, dy: values.insetByY))
card.makePath()
addSubview(card)
}
}
}
因此,如果我更改静态索引之一,则CardSubview中的图形将更改。
那就是我的想法,但是它不起作用,因为每次我更改索引时,每张卡都会被更改,并且不仅绘制一张新表格。
您将如何做,任何人都可以给我一些有关我的代码和小费的想法?
最佳答案
不需要card
中的CardBoardView
属性。您正在创建一整套CardSubview
实例,因此拥有仅存储最后一个实例的属性没有任何意义。
您需要对代码进行几处更改。
完全删除您的card
属性及其didSet
块。
将点击手势的创建放入创建每个CardSubview
实例的循环中。
在循环中使用局部变量。
更新tab
方法以从手势识别器获取卡片视图。
这是更新的代码:
class CardBoardView: UIView {
static var index1 = 1
static var index2 = 1
static var index3 = 1
static var index4 = 3
public var cells = 12
let values = Values()
struct Values {
public let ratio:CGFloat = 2.0
public let insetByX:CGFloat = 3.0
public let insetByY:CGFloat = 3.0
public let allAvailableCards = 81
}
@objc private func tab (recognizer: UITapGestureRecognizer) {
if recognizer.state == .ended {
if let card = recognizer.view as? CardBoardView {
card.isSelected = !card.isSelected
}
}
}
override func layoutSubviews() {
super.layoutSubviews()
var grid = Grid(layout: .aspectRatio(values.ratio), frame: self.bounds)
grid.cellCount = 12
for index in 0..<12 {
let card = CardSubview(frame: grid[index]!.insetBy(dx: values.insetByX, dy: values.insetByY))
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tab))
card.addGestureRecognizer(tapRecognizer)
card.makePath()
addSubview(card)
}
}
}
您可能需要通过尚未发布的其他一些代码访问每个
CardBoardView
实例。因此,您可能需要一个属性来存储所有卡。var cards = [CardBoardView]()
然后在创建每个卡的循环中,添加:
cards.append(card)