更改uiviewcontroller时动画停止

时间:2017-09-07 23:22:32

标签: ios swift animation swift3

我为第一个视图控制器编写选框文本,但更改控制器并返回第一个视图控制器选框文本不起作用。

第一个控制器代码:

@IBOutlet weak var marqueeText: UILabel!        

override func viewDidLoad() {

    super.viewDidLoad()
    UIView.animate(withDuration: 16.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in
        marqueeText.center = CGPoint(x: 0 -marqueeText.center.bounds.size.width / 2, y: text.center.y)
    }, completion:  { _ in })

}

1 个答案:

答案 0 :(得分:0)

请检查以下代码:

@IBOutlet weak var marqueeText: UILabel!

var marqueeTextPoint = CGPoint()
var stopAnim: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    marqueeTextPoint = marqueeText.center
    marquee(text: marqueeText)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.marqueeText.center = self.marqueeTextPoint
    marquee(text: marqueeText)
}

override func viewWillDisappear(_ animated: Bool) {
    stopAnim = true
}

func marquee (text: UILabel) {
    UIView.animate(withDuration: 16.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {
        text.center = CGPoint(x: 0 - text.bounds.size.width / 2, y: text.center.y)
    }, completion: { _ in
        if !(self.stopAnim) {
            self.marquee(text: text)
        } else  {
            text.layer.removeAllAnimations()
        }
    })
}