[英]UIView default properties and custom properties
我使用默認屬性創建了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屬性的視圖並使用自定義屬性重繪?
我無法完全理解您的問題。 我了解的是您想獲得特定的視圖,例如三角形視圖。
實現此目的的一種方法是在mainView中添加tag
,將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
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.