Swift:链UIView动画永远重复? Obj-C代码不可移植

时间:2015-05-06 14:45:34

标签: ios objective-c swift animation uiview

This post描述了如何在Objective-C中链接重复的UIView动画。但是,它需要performSelector,这似乎在Swift中不可用。

如何在Swift中链接重复的UIView动画?目标是创建一个动画来摇动/摆动UIView。

1 个答案:

答案 0 :(得分:0)

怎么样:

var wobble = true

func wobbleLeft() {

    if !wobble {
        return
    }

    UIView.animateWithDuration(0.5, animations: { () -> Void in
        // Animate wobble to left
        }) { (finished) -> Void in
            self.wobbleRight()
    }
}

func wobbleRight() {

    if !wobble {
        return
    }

    UIView.animateWithDuration(0.5, animations: { () -> Void in
        // Animate wobble to right
        }) { (finished) -> Void in
            self.wobbleLeft()
    }
}

首先致电wobbleLeft(),然后在完成后致电wobbleRight()。要停止摆动,只需设置wobble = false

即可