循环播放动画

时间:2015-05-17 23:40:00

标签: swift loops animation

我有这段代码

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //'img' is a picture imported in your project
    self.mrock.image = UIImage(named: "bird")
    self.rrock.image = UIImage(named: "rock2")
    self.lrock.image = UIImage(named: "rock3")
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)


    if rockNamesArray[1] == "rock2"  {
    let firstPos: CGFloat = 300.0
    UIView.animateWithDuration(3.0, animations: { () -> Void in
        self.mrock.frame = CGRectMake(167, 600, CGFloat(self.mrock.bounds.size.width), CGFloat(self.mrock.bounds.size.height))
    })}

    if rockNamesArray[2] == "rock3"  {
    let secondPos: CGFloat = 300.0
    UIView.animateWithDuration(3.0, animations: { () -> Void in
        self.rrock.frame = CGRectMake(220, 600, CGFloat(self.rrock.bounds.size.width), CGFloat(self.rrock.bounds.size.height))
    })}

    if rockNamesArray[0] == "bird"  {
    let thirdPos: CGFloat = 300.0
    UIView.animateWithDuration(3.0, animations: { () -> Void in
        self.lrock.frame = CGRectMake(115, 600, CGFloat(self.lrock.bounds.size.width), CGFloat(self.lrock.bounds.size.height))
    })}

而且我想知道如何制作它以便当png到达屏幕的末尾时,它会回到原来的位置,我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,您可以将完成结束传递给动态完成后将执行的UIView.animateWithDuration。在闭包中,您可以使视图返回其原始位置,例如:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let originalFrame = lrock.frame

    UIView.animateWithDuration(3.0, animations: {
        lrock.frame = CGRect(origin: CGPoint(x: 115, y: 600), size: lrock.frame.size)
    },
    completion: { _ in
        lrock.frame = originalFrame
    })
}