简体   繁体   English

Swift UIView.animateWithDuration和循环

[英]Swift UIView.animateWithDuration and For-Loops

I have a group of UIImageViews that are stored in an array. 我有一组存储在数组中的UIImageViews。 I want to animate a certain amount of these ImageViews in the same way. 我想以相同的方式为一定数量的这些ImageView设置动画。 Thus, I have been trying to use the following code: 因此,我一直在尝试使用以下代码:

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)

However at this line: 但是在这一行:

for index in startingIndex..< lowerViews.count {

Xcode gives me the error: Xcode给我错误:

Expected '{' to start the body of each for-loop 预期'{'开始每个for循环的主体

However, I don't believe that this is really the issue. 但是,我不认为这确实是问题。 This seems to me that it is an arbitrary syntax error that Xcode gets because I am using the for-loop inside the 'animation' parameter. 在我看来,这是Xcode遇到的任意语法错误,因为我在'animation'参数内使用了for循环。 Since I am still learning a lot about Swift I do not know why this wouldn't work, and so if this assumption is correct, I would like to know why and how I can get around this. 由于我仍在学习有关Swift的很多知识,我不知道为什么这行不通,因此,如果这个假设正确,我想知道为什么以及如何解决这个问题。

If this is not the case, please let me know, because either way I need to get around the problem. 如果不是这种情况,请让我知道,因为无论哪种方式,我都需要解决该问题。

Thanks in advance 提前致谢

This is a tricky error (note spaces around ..< ). 这是一个棘手的错误(请注意..<周围的空格)。

for index in startingIndex ..< lowerViews.count {

will work or 将工作或

for index in startingIndex..<lowerViews.count {

will work but: 可以工作,但是:

for index in startingIndex..< lowerViews.count {

won't work. 将无法正常工作。

The reason for this is the fact that when startingIndex..< is used, then ..< is considered to be a postfix (unary) operator (not an infix operator). 原因是这样的事实,当使用startingIndex..<时, ..<被认为是后缀(一元)运算符(而不是中缀运算符)。 Therefore the whole expression stops making sense and you start getting strange errors. 因此,整个表达式不再有意义,您将开始遇到奇怪的错误。

Also see what are the rules for spaces in swift 另请参阅快速移动空间的规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM