在For循环中使用performSelector?苹果手机

时间:2022-12-17 19:40:34

I have an array (called array in the code below) which contains a number of MyView objects. I am trying to iterate through these objects in a For loop and add them as a subview one by one, each after a delay of one second. The problem with the code below is that all the objects are added at once after a delay of one second. Can anyone suggest how I can correct ?

我有一个数组(在下面的代码中称为数组),其中包含许多MyView对象。我试图在For循环中迭代这些对象并逐个添加它们作为子视图,每个都延迟一秒。下面的代码的问题是在延迟一秒后立即添加所有对象。任何人都可以建议我如何纠正?

Thank you in advance.

先感谢您。

- (void)startMethod {

    for (MyView * myview in array) {

        [self performSelector:@selector(addSubView:) withObject:myview afterDelay:1];
    }
}

- (void)addSubView : (UIView *)view {
    [soundController playSound];
    [self.view addSubview:view];
}

4 个解决方案

#1


1  

The time to execute the loop isn't enough to delay selectors perform. You probably need to delay yourself using for example a counter.

执行循环的时间不足以延迟选择器执行。您可能需要使用例如计数器来延迟自己。

-(void)startMethod {
    NSUInteger i = 0;
    for (MyView * myview in array) {
        i += 1;
        [self performSelector:@selector(addSubView:)
                   withObject:myview
                   afterDelay:i];
    }
}

#2


0  

- (void)startMethod {

  int i = 1;
  for (MyView * myview in array) {
    [self performSelector:@selector(addSubView:) withObject:myview afterDelay:i];
    i++;
  }
}

#3


0  

Actually, its quite simple:

实际上,它非常简单:

- (void)startMethod {
    int seconds = 0;
    for (MyView * myview in array) {
        [self performSelector:@selector(addSubView:) withObject:myview afterDelay:++seconds];
    }
}

#4


0  

Simple solution: increase the delay by one second after each iteration of the loop.

简单的解决方案:在循环的每次迭代后将延迟增加一秒。

#1


1  

The time to execute the loop isn't enough to delay selectors perform. You probably need to delay yourself using for example a counter.

执行循环的时间不足以延迟选择器执行。您可能需要使用例如计数器来延迟自己。

-(void)startMethod {
    NSUInteger i = 0;
    for (MyView * myview in array) {
        i += 1;
        [self performSelector:@selector(addSubView:)
                   withObject:myview
                   afterDelay:i];
    }
}

#2


0  

- (void)startMethod {

  int i = 1;
  for (MyView * myview in array) {
    [self performSelector:@selector(addSubView:) withObject:myview afterDelay:i];
    i++;
  }
}

#3


0  

Actually, its quite simple:

实际上,它非常简单:

- (void)startMethod {
    int seconds = 0;
    for (MyView * myview in array) {
        [self performSelector:@selector(addSubView:) withObject:myview afterDelay:++seconds];
    }
}

#4


0  

Simple solution: increase the delay by one second after each iteration of the loop.

简单的解决方案:在循环的每次迭代后将延迟增加一秒。