0

我有一个 UIView,我要添加 15 个 UIButton。如果我写了这样的代码

for(int i =0; i<15;i++)
{
    [self.view addSubview:[self.buttonsArray objectAtIndex;i]];
}

但我想一个接一个地查看 UIButtons。类似动画效果的东西。如何实现它。

4

5 回答 5

2

尝试这个:

在 .h 文件中

NSTimer *timer;
int buttonNo;

@property (nonatomic,retain) NSTimer *timer;

在 .m 文件中

@synthesize timer;

-(void) viewDidLoad {
    [super viewDidLoad];
    //start your timer where you write your for loop
    buttonNo = 0;
    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(addbuttons) userInfo:nil repeats:YES];
} 


-(void) addbuttons {
    [self.view addSubview:[self.buttonsArray objectAtIndex;buttonNo]];
    buttonNo++;
    if (buttonNo == 15) {
        [timer invalidate];
        timer = nil;
    }
}
于 2013-07-15T12:24:21.353 回答
1

我会这样做:

const float fadein_duration = 0.3f;
const float time_between_fadein = 0.15f;

int index = 0;
for (UIButton* button in self.buttonsArray)
{
    [self.view addSubview:button];
    button.alpha = 0.0f;
    [UIView animateWithDuration:fadein_duration delay:(index * time_between_fadein) options:0 animations:^{
        button.alpha = 1.0f;
    } completion:^(BOOL finished) {
        // Do something maybe?
    }];
    index++;
 }

目前,动画是淡入的,但您应该能够使其适应其他类型的动画。

现在有 2 个常量可用于控制淡入淡出的持续时间,以及 2 个按钮出现之间的时间。

确保在创建按钮时为按钮设置正确的框架。否则,你只会得到一堆重叠的按钮。如果需要,可以在将按钮添加到子视图时动态设置按钮的位置。

此外,我更改了循环模式以在数组上使用快速迭代器,并手动保持增量索引。

于 2013-07-15T12:30:49.803 回答
0

我不确定你的意思。至于动画效果,你可能想实现某种类型的定时器,否则它们会同时添加

于 2013-07-15T12:25:38.013 回答
0

如果你想要一种动画的淡入淡出,那么你可以实现这样的东西 -

for(int i =0; i<15;i++)
{
        [self.view addSubview:[self.buttonsArray objectAtIndex;i]];

        [self performSelector:@selector(showBtn:) withObject:self.buttonsArray objectAtIndex;i] afterDelay:interval];

        interval += 0.5f;
}

- (void) showBtn:(UIButton*) btn
{    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5f];
    [btn setAlpha:1.0f];
    [UIView commitAnimations];
}

我假设数组中所有按钮的默认 alpha 为 0。

于 2013-07-15T12:35:29.483 回答
-1

请试试这个代码

for(int i =0; i<15;i++) 
{

    [self.view addSubview:[self.buttonsArray objectAtIndex;i]]; 
    [self performSelector:@selector(your animated methode) withObject:nil afterDelay:0.1];

}
于 2013-07-15T12:18:32.260 回答