更改动画后文本消失

时间:2018-12-21 02:55:15

标签: ios swift animation

我正在尝试构建Quiz iOS应用,当用户完成问题后,它就会滑开并由新问题代替。但是,当我用一个新的问题替换文本时,它消失了(例如,动画完成并可以完美运行,然后在该文本消失后立即显示)。

 func slideOutThenIn() {
    viewUpperContainer.transform = CGAffineTransform(translationX: 0, y: 0)
    viewLowerContainer.transform = CGAffineTransform(translationX: 0, y: 0)

    UIView.animate(withDuration: 1, delay: 0, options: .curveEaseInOut, animations: {

        self.viewUpperContainer.transform = CGAffineTransform(translationX: -self.view.frame.width, y: 0)
        self.viewLowerContainer.transform = CGAffineTransform(translationX: -self.view.frame.width, y: 0)

    }) { (_) in

        self.viewUpperContainer.frame.origin.x = self.view.frame.width
        self.viewLowerContainer.frame.origin.x = self.view.frame.width

        UIView.animate(withDuration: 1, delay: 0, options: .curveEaseInOut, animations: {

            self.viewUpperContainer.transform = CGAffineTransform(translationX: (-self.view.frame.width * 2), y: 0)
            self.viewLowerContainer.transform = CGAffineTransform(translationX: (-self.view.frame.width * 2), y: 0)

        }, completion: {finished in
            self.questionLabel.text = self.allQuestions.list[self.questionNumber].questionText
        })
    }

}

我希望在动画过程中更改文本,但是现在我很高兴在动画完成后立即更改文本而不会消失。

2 个答案:

答案 0 :(得分:0)

尝试-

func slideOutThenIn() {

    //initial position 0
    self.viewUpperContainer.frame.origin.x = 0
    self.viewLowerContainer.frame.origin.x = 0

    UIView.animate(withDuration: 1, delay: 0, options: .curveEaseInOut, animations: {

       //move out 1
       self.viewUpperContainer.frame.origin.x = self.view.frame.width
       self.viewLowerContainer.frame.origin.x = self.view.frame.width

    }) { (_) in

        //change text
        self.questionLabel.text = self.allQuestions.list[self.questionNumber].questionText

        UIView.animate(withDuration: 1, delay: 0, options: .curveEaseInOut, animations: {

            //move in -1
            self.viewUpperContainer.frame.origin.x = 0
            self.viewLowerContainer.frame.origin.x = 0

        }, completion: {finished in

           //ready for use 0
        })
    }
}

答案 1 :(得分:0)

这是对我有用的答案。

func slideOutThenIn() {
    viewUpperContainer.transform = CGAffineTransform(translationX: 0, y: 0)
    viewLowerContainer.transform = CGAffineTransform(translationX: 0, y: 0)

    UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {

        self.viewUpperContainer.transform = CGAffineTransform(translationX: -self.view.frame.width, y: 0)
        self.viewLowerContainer.transform = CGAffineTransform(translationX: -self.view.frame.width, y: 0)

    }) { (_) in

        self.viewUpperContainer.transform = CGAffineTransform(translationX: self.view.frame.width, y: 0)
        self.viewLowerContainer.transform = CGAffineTransform(translationX: self.view.frame.width, y: 0)

        self.questionLabel.text = self.allQuestions.list[self.questionNumber].questionText

        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {

            self.viewUpperContainer.transform = CGAffineTransform(translationX: 0, y: 0)
            self.viewLowerContainer.transform = CGAffineTransform(translationX: 0, y: 0)

        }, completion: {finished in
        })
    }

}