首页 > 解决方案 > 更改 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)


    })


}

标签: iosswiftconstraintsios-animations

解决方案


推荐阅读