快速UIView animateWithDuration具有重复和自动反转 [英] swift UIView animateWithDuration with repeat and autoreverse

查看:158
本文介绍了快速UIView animateWithDuration具有重复和自动反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,这是我有史以来第一个提出的问题....
我想用2秒的时间收缩一个球,然后将其生长5秒。
我的问题是第二个持续时间被忽略了(球收缩2秒并增长2秒)。
我希望有人能帮助我。

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.

这是我的尝试:

    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)
            })
    })


推荐答案

我的回答多亏了Matt的帮助(持续时间,原始问题的变量已更改):

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

快捷键2

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
)

快速3、4、5

let duration = 6.0
let delay = 0.0
UIView.animateKeyframes(withDuration: duration, delay: delay, options: [.repeat], animations: {
    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/3, animations: {
        ball.frame = CGRect(x: screenWidth/8*3, y: screenHeight/8*3, width: screenWidth/4, height: screenWidth/4)
    })
    UIView.addKeyframe(withRelativeStartTime: 1/3, relativeDuration: 2/3, animations: {
        ball.frame = CGRect(x: screenWidth/4, y: screenHeight/4, width: screenWidth/2, height: screenWidth/2)
    })
    }, completion: nil
)

这篇关于快速UIView animateWithDuration具有重复和自动反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆