更改Label后,Swift动画的行为异常

时间:2018-11-14 09:09:10

标签: ios swift constraints ios-animations

我目前正在为ios应用程序制作动画。触发后,三个块(带边框的UIView)将向下移动,并从左侧进入一个新块。

Working Version

它非常有效,直到在动画开始之前我更改标签的文本(块编号)为止。块在完全不同的位置生成,然后移回起始位置。他们还更改了订单。

enter image description here

以下代码显示了我的块类。对于动画,我使用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)


    })


}

0 个答案:

没有答案