在ViewController.swift中,我成功地让一个方框从一个点移动到另一个点的动画。我认为将其循环应该很容易,让方框动画到一个位置,然后回到原始位置并再次循环。我已经设法将对象移动到一个位置,在“complete”中将其移回,但这并不能使它循环。如何实现这一点?
我想也许这个方法可以奏效,但我真的不确定:
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)
})
})
coloredSquare.center
属性来实现,而不是框架。要将其居中显示在视图中,请尝试coloredSquare.center = view.center
,或者更好的是coloredSquare.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
。 - MazyodautoresizingMask
属性设置为可伸缩边距。 - MazyodcoloredSquare.layer.removeAllAnimations()
。 - Mazyod