swift UIView animateWithDuration with repeat和autoreverse

时间:2016-01-10 19:36:57

标签: ios swift animation animatewithduration

我是swift的新手,这是我的第一个问题.... 我想缩短一个持续时间为2秒的球,然后将其长度持续5秒。 我的问题是第二个持续时间被忽略(球缩小2秒并增长2秒)。 我希望有人可以帮助我。

这是我的尝试:

    let ball = UIView()
    ball.frame = CGRectMake(50, 50, 50, 50)
    ball.backgroundColor = UIColor.blueColor()
    ball.layer.cornerRadius=25
    relaxContainer.addSubview(ball)

    UIView.animateWithDuration(2.0, delay:0, options: [.Repeat, .Autoreverse], animations: {
        ball.frame = CGRectMake(50, 50, 20, 20)
        }, completion: { finished in
            UIView.animateWithDuration(5.0, animations: {
                ball.frame = CGRectMake(50, 50, 50, 50)
            })
    })

1 个答案:

答案 0 :(得分:1)

我的回答感谢Matt的帮助(持续时间,原始问题的变量被更改):

    let duration = 6.0
    let delay = 0.0
    UIView.animateKeyframesWithDuration(duration, delay: delay, options: [.Repeat], animations: {
        UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/3, animations: {
            ball.frame = CGRectMake(screenWidth/8*3, screenHeight/8*3, screenWidth/4, screenWidth/4)
        })
        UIView.addKeyframeWithRelativeStartTime(1/3, relativeDuration: 2/3, animations: {
            ball.frame = CGRectMake(screenWidth/4, screenHeight/4, screenWidth/2, screenWidth/2)
        })
        }, completion: nil
    )