iOS按下时禁用动画

时间:2017-01-26 11:53:05

标签: ios swift3

当我按下后退按钮时,动画再次开始,我的屏幕上的按钮松动。

这是一个更好理解的GIF:

https://i.gyazo.com/9d2ec00152c614c94a2ab2c797e86e12.gif

eoverride func viewDidLoad() {
    super.viewDidLoad()

    std.center.x += view.bounds.width
    prtl.center.x += view.bounds.width
    .
    .

}

override func viewWillAppear(_ animated: Bool)
{
    UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: {
        self.std.center.x -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 0.5, delay: 0.6, options: [], animations: {
        self.prtl.center.x -= self.view.bounds.width
    }, completion: nil)
    .
    .
}

如何再次停止动画启动以防止屏幕上的按钮丢失。

谢谢!

3 个答案:

答案 0 :(得分:1)

override func viewDidLoad() {
    super.viewDidLoad()

    std.center.x += view.bounds.width
    prtl.center.x += view.bounds.width
    .
    .
    UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: {
        self.std.center.x -= self.view.bounds.width
    }, completion: nil)

    UIView.animate(withDuration: 0.5, delay: 0.6, options: [], animations: {
        self.prtl.center.x -= self.view.bounds.width
    }, completion: nil)
}

从viewWillAppear中删除代码并尝试此操作。

答案 1 :(得分:0)

如果您想在视图加载时第一次设置动画视图,则需要在viewDidLoad()内调用您的代码

//MARK: - Life cycle
override func viewDidLoad() {
    super.viewDidLoad()

    std.center.x += view.bounds.width
    prtl.center.x += view.bounds.width

    // Call your animation related code here
    animateView()
}

func animateView(){
     UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: {
         self.std.center.x -= self.view.bounds.width
     }, completion: nil)

    UIView.animate(withDuration: 0.5, delay: 0.6, options: [], animations: {
        self.prtl.center.x -= self.view.bounds.width
     }, completion: nil)
}

答案 2 :(得分:0)

override func viewWillAppear(_ animated: Bool)
{
    UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: {
        self.std.center.x = 0
    }, completion: nil)

    UIView.animate(withDuration: 0.5, delay: 0.6, options: [], animations: {
        self.prtl.center.x = 0
    }, completion: nil)
    .
    .
}

或使用BOOL检查

let shouldAnimate = (self.prtl.center.x != 0)

if shouldAnimate ....