gpt4 book ai didi

ios - 如何从一个 UIView 类中制作不同的绘制 View ?

转载 作者:行者123 更新时间:2023-11-30 11:30:27 26 4
gpt4 key购买 nike

我正在制作一个类似于“Set”游戏的 iOS 应用/游戏。

为此,我需要创建多个 View ,因此我创建了此类:

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

....

所以你需要知道这会创建一个带有矩形、三角形或圆形等的 View ......

现在我想将其中 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 中的绘图将会更改。

这是我的想法,但它不起作用,因为每次我更改索引时,每张卡片都会更改并绘制新的表单,而不仅仅是一张。 你会怎么做,有人能给我一些关于我的代码的想法和一些提示吗?

最佳答案

CardBoardView 中不需要 card 属性。您正在创建一整套 CardSubview 实例,因此拥有仅存储最后一个实例的属性是没有意义的。

您需要对代码进行一些更改。

  1. 完全删除您的 card 属性及其 didSet block 。

  2. 将点击手势的创建放入创建每个 CardSubview 实例的循环中。

  3. 在循环中使用局部变量。

  4. 更新 tab 方法以从手势识别器获取卡片 View 。

这是更新后的代码:

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)

关于ios - 如何从一个 UIView 类中制作不同的绘制 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50306999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com