删除子视图然后将其重新添加 [英] Removing a subview then adding it back
问题描述
我有一个带有渐变子视图的 UICollectionViewCell
。
在我的视图控制器中,我有一些按钮依次对CoreData进行排序,然后对 UICollectionView
上的 reloadData()
进行排序。
为了避免一次又一次绘制渐变子视图(就像过去那样),我在 prepareForReuse中实现了
。 removeFromSuperview()
()
在那里,我还实现了一个跟踪梯度存在的标记,因此当单元加载时我仍然添加新的梯度。
但是,删除渐变后,我的 willMove(toSuperview:)
不起作用,并且没有出现渐变视图。
我的逻辑怎么了?
I have a UICollectionViewCell
with a gradient subview.
In my view controller, I have buttons that are sorting CoreData and then reloadData()
on the UICollectionView
.
To avoid my gradient subview to be drawn again and again (as it happened in the past), I implement removeFromSuperview()
in prepareForReuse()
.
There I also implement a flag that keeps track of gradient existence so I still add a new gradient when cell is load.
However after removing the gradient, my willMove(toSuperview: )
doesn't work, and gradient view doesn't appear.
What's wrong with my logic?
class CollectionCell: UICollectionViewCell {
@IBOutlet weak var mealImg: UIImageView!
@IBOutlet weak var mealTitleLbl: UILabel!
@IBOutlet weak var gradientView: UIView!
var gradientWasRemoved = false
func configureCell(meal: Meal) {
mealTitleLbl.text = meal.title
let img = meal.getMealImage()
mealImg.image = img
addGradient()
}
func addGradient () {
let gradient = CAGradientLayer()
gradient.frame = gradientView.bounds
let topColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1)
let botomColor = UIColor.clear
gradient.colors = [topColor.cgColor, botomColor.cgColor]
gradientView.layer.insertSublayer(gradient, at: 0)
if gradientWasRemoved == true {
gradientView.willMove(toSuperview: self)
}
}
override func prepareForReuse() {
super.prepareForReuse()
gradientView.removeFromSuperview()
gradientWasRemoved = true
}
}
推荐答案
我能够通过以下方式修复逻辑。
在UICollectionViewCell类中:
I was able to fix the logic with following. In UICollectionViewCell class:
import UIKit
class CollectionCell: UICollectionViewCell {
@IBOutlet weak var mealImg: UIImageView!
@IBOutlet weak var gradientView: UIView!
@IBOutlet weak var mealTitleLbl: UILabel!
var gradientWasRemoved = false
func configureCell(meal: Meal) {
mealTitleLbl.text = meal.title
let img = meal.getMealImage()
mealImg.image = img
addGradient()
}
func addGradient () {
let gradient = CAGradientLayer()
gradient.frame = gradientView.bounds
let topColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1)
let botomColor = UIColor.clear
gradient.colors = [topColor.cgColor, botomColor.cgColor]
if gradientWasRemoved == false {
gradientView.layer.insertSublayer(gradient, at: 0)
} else if gradientWasRemoved == true {
self.addSubview(gradientView)
}
}
override func prepareForReuse() {
super.prepareForReuse()
gradientView.removeFromSuperview()
gradientWasRemoved = true
}
}
在 prepareForReus中e()
我没有删除 gradientView
,而是将其从Superview中删除了。我在那里设置了将其删除的标志。
因为我没有 nil
我的 gradientView
,所以我能够运行 addGradient ()
并访问 gradientView.bounds
来创建新的 CGGradientLayer
。
在向 gradientView
添加新图层之前,我执行了 if
检查。如果确实删除了 gradientView
,那么我们就不会添加新的 CGGradientLayer
,而只需添加我们的 back
。
这样,我们只将 CGGradientLayer
添加到我们的gradientView中一次。
In prepareForReuse( )
I didn't delete gradientView
, I removed it from the Superview. I set the flag there that it was removed.
Since I didn't nil
my gradientView
i was able to run addGradient()
and access gradientView.bounds
for creation of new CGGradientLayer
.
Right before adding a new layer to gradientView
, I performed if
check. If we did remove gradientView
then we don't add a new CGGradientLayer
but simply put our gradientView
back.
This way we add CGGradientLayer
to our gradientView only once.
我了解到,通过从Superview中删除视图,这些视图仍然有效并且可以进行编辑。
I learned that by removing views from Superview they are still alive and can be edited.
感谢@Luis和@LeoDabus。
Thank you @Luis and @LeoDabus for your contribution.
这篇关于删除子视图然后将其重新添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!