首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UIView.animateWithDuration快速循环动画

UIView.animateWithDuration快速循环动画
EN

Stack Overflow用户
提问于 2014-12-27 02:23:40
回答 1查看 53.9K关注 0票数 42

在ViewController.Swift中,我设法让一个长方体从一个点动画到另一个点。我想这会很容易循环,这样长方体将动画到一点,然后动画回到它的原始位置,然后再次循环。我已经成功地将对象移动到一个位置,并在“完全”状态下再次将其移回,但这并不会使I循环。如何才能做到这一点?

我想也许这能行得通,但老实说我不知道:

代码语言:javascript
复制
let boxmoves = [CGRect(x: 120, y: 220, width: 100, height: 100), CGRect(x: 120, y: 120, width: 100, height: 100)]
for boxmove in boxmoves {
    coloredSquare.frame = boxmove
}

我如何根据设备宽度将其居中(我假设其中涉及到一些数学运算?)?

我的代码:

代码语言:javascript
复制
let coloredSquare = UIView()

coloredSquare.backgroundColor = UIColor.blueColor()

coloredSquare.frame = CGRect(x: 120, y: 120, width: 100, height: 100)

self.view.addSubview(coloredSquare)

// found repeate but this will not animate as I want.
//UIView.animateWithDuration(2.0, delay: 0.2, options: UIViewAnimationOptions.Repeat, animations: {
UIView.animateWithDuration(2.0, animations: {

    coloredSquare.frame = CGRect(x: 120, y: 220, width: 100, height: 100)

    }, completion: { finished in
        UIView.animateWithDuration(2.0, animations: {
        coloredSquare.frame = CGRect(x: 120, y: 120, width: 100, height: 100)
        })
})
EN

回答 1

Stack Overflow用户

发布于 2018-09-11 19:52:15

代码语言:javascript
复制
UIView.animate(withDuration: 3.0,
                           delay: 0.0,
                           options: [.curveLinear, .repeat],
                           animations: { () -> Void in
                           coloredSquare.frame = CGRect(x: 120, y: 220, width: 100, height: 100)

}, completion: { (finished: Bool) -> Void in

})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27660540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档