我使用默认属性创建了UIView
var color = Label.Color.black { didSet{ setNeedsDisplay() }}
var count = Label.Count.one.rawValue { didSet{ setNeedsDisplay() }}
var shading = Label.Shading.empty { didSet{ setNeedsDisplay() }}
var shape = Label.Shape.triangle { didSet{ setNeedsDisplay() }}
var isSelected = false
然后我在ViewController中使用相同的UIView添加一些子视图
func addCardsOnGridView() {
grid.frame = cardsView.bounds
grid.cellCount = game.cardsOnDeck.count
for cardView in cardsView.subviews{
cardView.removeFromSuperview()
}
for index in 0..<grid.cellCount {
if let cellFrame = grid[index] {
let card = game.cardsOnDeck[index]
let cardView = CardsView(frame: cellFrame.insetBy(dx: CardSize.inset, dy: CardSize.inset))
cardView.color = card.label.color
cardView.count = card.label.count.rawValue
cardView.shape = card.label.shape
cardView.shading = card.label.shading
//cardView.isSelected = card.isSelected
//print(cardsView.isSelected)
cardsView.addSubview(cardView)
} else {
print("grid[\(index)] doecnt exist")
}
}
}
然后我得到了默认的UIView,它是Superview和Superview之上的子视图:
如何删除具有默认UIView属性的视图并使用自定义属性重绘?
答案 0 :(得分:0)
我无法完全理解您的问题。我了解的是您想获得特定的视图,例如三角形视图。
一种实现此目的的方法是,将tag
分配给如下所示的每个子视图,如下所示:
aView.tag = 6
,然后当您想要获得子视图时,可以使用下面的viewWithTag
函数
if let aCustomView = mainView.viewWithTag(6) as? CustomView {
aCustomView.myColor = .blue
}
获取自定义视图的另一种方法是循环思想子视图,然后尝试将视图投射到如下所示的自定义类中
mainView.subviews.forEach({ (aView) in
if aView.tag == 6,
let customView = aView as? CustomView {
customView.myColor = .blue
}
})