ios - 更改 Label 后 Swift Animation 的行为很奇怪
问题描述
我目前正在为我的 ios 应用程序制作动画。当它被触发时,三个块(带边框的 UIView)向下移动,一个新块从左侧进入。
直到我在动画开始之前更改标签的文本(块编号)之前,它都可以正常工作。块在完全不同的位置生成并移回起始位置。他们也改变了他们的顺序。
以下代码显示了我的块类。对于动画,我使用 getBlockView 和“view.frame = GCRect(...)”。为了更改标签文本,我使用 setBlockName(number: String) 函数。
目前我不知道是什么导致了这种情况,并感谢每一个可能导致这种奇怪行为的建议。
class myBlock {
var positionID: Int
var blockNumber: String
let blockView: UIView
let blockNumberLabel: UILabel
init(blockNumber: String, heightScreen: CGFloat, widthScreen: CGFloat, positionID: Int) {
self.positionID = positionID
self.blockNumber = blockNumber
blockView = {
let view = UIView()
view.autoSetDimension(.height, toSize: heightScreen / 6)
view.autoSetDimension(.width, toSize: widthScreen - 80)
view.layer.borderWidth = 3
view.layer.borderColor = UIColor.MyTheme.primaryColor1.cgColor
return view
}()
blockNumberLabel = {
let label = UILabel()
label.textColor = UIColor.MyTheme.primaryColor1
label.font = UIFont(name: "ArialMT", size: 20)
label.numberOfLines = 2
label.textAlignment = .left
label.adjustsFontForContentSizeCategory = true
label.text = "Block Number: \(blockNumber)"
label.isUserInteractionEnabled = false
return label
}()
blockView.addSubview(blockNumberLabel)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .top, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .left, withInset: 5.0)
blockNumberLabel.autoPinEdge(toSuperviewEdge: .right, withInset: 5.0)
}
func getBlockView() -> UIView{
return blockView
}
func setBlockName(number: String) {
self.blockNumberLabel.text = "Block Number: \(number)"
}
func getBlockLabel() -> UILabel{
return blockNumberLabel
}
func getID() -> Int {
return positionID
}
func setID(id: Int) {
self.positionID = id
print("\(blockNumberLabel.text!): \(id)")
}
}
动画代码:
private func moveBlocksDown(blockNumber: String) {
var resetBlock: UIView!
let animationHeight = (self.heightScreen / 6) + (self.blockDistance)
UIView.animate(withDuration: 2.0, animations: {
for var block in self.blockList {
let id = block.getID()
let blockView = block.getBlockView()
if block.getID() == 0 {
block.setBlockName(number: blockNumber)
}
print(id)
switch id {
case 0:
blockView.frame = CGRect(x: self.topX, y: self.topY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 1:
blockView.frame = CGRect(x: self.topX, y: self.middleY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 2:
blockView.frame = CGRect(x: self.topX, y: self.bottomY, width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
case 3:
blockView.frame = CGRect(x: self.topX, y: (self.bottomY + animationHeight), width: blockView.frame.width, height: blockView.frame.height)
print(block.getBlockLabel().text!)
resetBlock = blockView
default:
print("Unknown ID")
}
print("NewID \((id + 1) % 4)")
block.setID(id: (id + 1) % 4)
}
}, completion: { finish in
resetBlock.frame = CGRect(x: self.newX, y: self.newY, width: resetBlock.frame.width, height: resetBlock.frame.height)
})
}
解决方案
推荐阅读
- python - 如何忽略其他渠道?
- windows - 完全隐藏服务器名称 Apache Windows
- python - 在 python 中使用 Plotly 绘制基于 Long 格式的图形线
- javascript - 如何从 React Native 中的另一个类组件中设置状态?
- android - 验证和加载密钥失败 Mifare Classic 4K
- html - 在不使用 CSS 的情况下对齐 html 中的输入字段
- javascript - Apache Echarts:自动选择工具箱功能
- java - 如何根据 OpenXava 的引用值定义每一行的颜色/样式?
- python-3.x - python中的任何浮点到单精度浮点MIL-STD-1750A标准
- lucene - lucene如何存储文档?