我有一组UIImageViews,它们存储在一个数组中。我想以同样的方式为这些ImageViews设置一定数量的动画。因此,我一直在尝试使用以下代码:
var lowerViews: [UIImageView] = [imageView1, imageView2, imageView3, imageView4]
var startingIndex = 1;
UIView.animateWithDuration(0.3, delay: 0.1, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
for index in startingIndex..< lowerViews.count {
lowerViews[index].frame.origin.y += 100
}
}, completion: nil)
但是在这一行:
for index in startingIndex..< lowerViews.count {
Xcode给了我错误:
需要"{"来启动每个for循环的正文
然而,我不认为这是真正的问题。在我看来,这是Xcode出现的任意语法错误,因为我在"animation"参数中使用了for循环。由于我对Swift还有很多了解,我不知道为什么这不起作用,所以如果这个假设是正确的,我想知道为什么以及如何绕过这个问题。
如果不是这样,请告诉我,因为不管怎样,我都需要解决这个问题。
提前感谢
这是一个棘手的错误(注意..<
周围的空格)。
for index in startingIndex ..< lowerViews.count {
将工作或
for index in startingIndex..<lowerViews.count {
将工作,但:
for index in startingIndex..< lowerViews.count {
不会起作用。
原因是当使用startingIndex..<
时,..<
被认为是后缀(一元)运算符(而不是中缀运算符)。因此,整个表达式失去了意义,你开始出现奇怪的错误。
另请参阅swift 中的空格规则