swift UIView animateWithDuration with repeat and autoreverse

不想你离开。 提交于 2019-12-08 04:31:57

问题


I am new to swift and this is my first question ever .... I would like to shrink a ball with duration 2 seconds, and then grow it for a duration of 5 seconds. My problem is that the second duration is ignored (ball shrinks for 2 seconds and grows for 2 seconds). I hope someone can help me.

This is my attempt:

    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:


My Answer thanks to help by Matt (duration times, variables from original question were changed):

    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
    )


来源:https://stackoverflow.com/questions/34709916/swift-uiview-animatewithduration-with-repeat-and-autoreverse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!