在ViewController.Swift中,我设法让一个长方体从一个点动画到另一个点。我想这会很容易循环,这样长方体将动画到一点,然后动画回到它的原始位置,然后再次循环。我已经成功地将对象移动到一个位置,并在“完全”状态下再次将其移回,但这并不会使I循环。如何才能做到这一点?
我想也许这能行得通,但老实说我不知道:
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
}
我如何根据设备宽度将其居中(我假设其中涉及到一些数学运算?)?
我的代码:
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)
})
})
发布于 2018-09-11 19:52:15
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
})
https://stackoverflow.com/questions/27660540
复制相似问题