我有一个从另一个推送的 CollectionViewViewController。此 CollectionViewViewController 的 View 有两个 subview :内部带有标签的简单 UIView 和 UICollectionView。当它第一次出现时,一切正常,渐变显示正确。但是当我弹出这个 View Controller(因此它被释放)并从同一个父 View Controlle 再次推送它时,渐变没有显示。
单元格如下出队:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PageScrollGeminiCellIdentifier, for: indexPath) as! PageScrollGeminiCell
let item = viewModel.items[indexPath.item]
cell.configure(withTitle: item.name, description: item. description, imageURL: item.imageUrl, gradientColor: item.color, theme: theme)
mainView.geminiView.animateCell(cell)
return cell
}
下面是我在单元格的类方法和单元格生命周期中所做的:
private var gradientColor: UIColor?
private var gradientLayer: CAGradientLayer?
[...]
override init(frame: CGRect) {
super.init(frame: .zero)
setupView()
}
required init?(coder aDecoder: NSCoder) {
return nil
}
override func layoutSubviews() {
if gradientLayer != nil {
gradientView.layer.sublayers = nil
}
if let color = gradientColor {
let gradientPoints = (CGPoint(x: 0.0, y: 0.15), CGPoint(x: 0.0, y: 1.0))
self.gradientLayer = gradientView.applyGradient(with: [color, color.withAlphaComponent(0.08)], gradient: .vertical(gradientPoints))
}
}
// MARK: Private
private func setupView() {
clipsToBounds = true
layer.cornerRadius = 15
backgroundColor = .clear
addSubview(containerView)
containerView.addSubview(backgroundImageView)
containerView.addSubview(gradientView)
containerView.addSubview(labelsContainerView)
labelsContainerView.addSubview(titleLabel)
labelsContainerView.addSubview(descriptionLabel)
containerView.snp.makeConstraints { maker in
maker.edges.equalToSuperview()
}
backgroundImageView.snp.makeConstraints { maker in
maker.edges.equalToSuperview()
}
gradientView.snp.makeConstraints { maker in
maker.leading.trailing.top.bottom.equalToSuperview()
}
labelsContainerView.snp.makeConstraints { maker in
maker.leading.top.equalTo(16)
maker.trailing.equalTo(-16)
}
titleLabel.snp.makeConstraints { maker in
maker.top.leading.trailing.equalToSuperview()
}
descriptionLabel.snp.makeConstraints { maker in
maker.top.equalTo(titleLabel.snp.bottom).offset(8)
maker.leading.trailing.bottom.equalToSuperview()
}
}
// MARK: - Internal
func configure(withTitle title: String?, description: String?, imageURL: String?, gradientColor: UIColor?, theme: Themeable) {
backgroundImageView.setImage(withString: imageURL)
titleLabel.text = title
titleLabel.font = theme.font.h2
descriptionLabel.text = description
descriptionLabel.font = theme.font.b1
self.gradientColor = gradientColor
}
我还想向您展示 applyGradient 功能:
func applyGradient(with colours: [UIColor], gradient orientation: GradientOrientation) -> CAGradientLayer {
layoutSubviews()
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
gradient.cornerRadius = layer.cornerRadius
self.layer.insertSublayer(gradient, at: 0)
return gradient
}
我发现,当 View Controller 第一次呈现时,layoutSubviews() 方法被调用了两次,但每次下一次每个单元格只被调用一次。
现在我不确定我是否没有向我的单元插入超过 1 个子层,但我确定我正在清除子层阵列,所以我认为这不是问题。
最佳答案
不应该调用 layoutSubviews(),应用程序在需要时自行调用此函数(这就是为什么它在开始时被调用两次) 如 Swift 开发者网站所述: https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews
You should not call this method directly. If you want to force a layout update, call the setNeedsLayout() method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded() method.
尝试避免使用或调用 layoutSubviews(),相反,您可以尝试在 func 中设置 gradientLayer 变量并在需要调用时调用它,尝试下面的 setupView()
如果有帮助,请告诉我。
关于ios - CollectionViewCell 的 subview 的渐变层仅在 View CONtroller 首次接纳时存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55707469/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我的模型有defself.empty_building//stuffend我怎样才能对这个现有的进行rspec?,已经尝试过:describe"empty_building"dosubject{Building.new}it{shouldrespond_to:empty_building}endbutgetting:Failure/Error:it{shouldrespond_to:empty_building}expected#torespondto:empty_building 最佳答案 你有一个类方法self.empty_bu
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
有几种方法:first_or_create_by、find_or_create_by等,它们的工作原理是:与数据库对话以尝试找到我们想要的东西如果我们找不到,就自己做保存到数据库显然,并发调用这些方法可能会使两个线程都找不到它们想要的东西,并且在第3步中一个线程会意外失败。似乎更好的解决方案是,创建或查找即:提前在您的数据库中创建合理的唯一性约束。如果你想保存一些东西,就保存它如果有效,那就太好了。如果它因为RecordNotUnique异常而无法工作,它已经存在,太好了,加载它那么在什么情况下我想使用Rails内置的东西而不是我自己的(看起来更可靠)create_or_find?
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
我有一个类unzipper.rb,它使用Rubyzip解压文件。在我的本地环境中,我可以成功解压缩文件,而无需使用require'zip'明确包含依赖项但是在Heroku上,我得到一个NameError(uninitializedconstantUnzipper::Zip)我只能通过使用明确的require来解决问题:为什么这在Heroku环境中是必需的,但在本地主机上却不是?我的印象是Rails自动需要所有gem。app/services/unzipper.rbrequire'zip'#OnlyrequiredforHeroku.Workslocallywithout!class
是否可以在不实际下载文件的情况下检查文件是否存在?我有这么大的(~40mb)文件,例如:http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm这与ruby不严格相关,但如果发件人可以设置内容长度就好了。RestClient.get"http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm",headers:{"Content-Length"=>100} 最佳答案
当且仅当模型存在时,我才尝试更新模型的值。如果没有,我什么都不做。搜索似乎只返回更新或创建问题/答案,但我不想创建。我知道我可以用一个简单的方法来做到这一点:found=Model.find_by_id(id)iffoundupdatestuffend但是,我觉得有一种方法可以在一次调用中完成此操作,而无需分配任何临时本地值或执行if。如果记录不存在,我该如何编写一个Rails调用来更新记录而不出现嘈杂错误?最新的Rails3.x 最佳答案 您可以使用try在对find_by_id或where的结果调用update_attribut
类似的东西:defdomain_exists?(domain)#performcheck#returntrue|falseendputs"valid!"ifdomain_exists?("example.com") 最佳答案 require'socket'defdomain_exists?(domain)beginSocket.gethostbyname(domain)rescueSocketErrorreturnfalseendtrueend 关于ruby-如何使用Ruby检查域是否存