如何使 UIButton 按计划的时间间隔显示



所以我有一个定时延迟动作,这样当一个人按下按钮时,它会在设定的时间数字(5秒)后显示一个标签

-(IBAction)start{
Desc.text = @"Text appears";
[self performSelector:@selector(delay) withObject:nil afterDelay:5.0];
}
-(void)delay{
Desc2.text = @"Text to appear in 5 seconds";
[self performSelector:@selector(delayA) withObject:nil afterDelay:5.0];
}

至于我试图制作的下一行代码,而不是在预定的时间间隔后会出现标签,而是尝试让一个按钮在 5 秒内出现。

谁能帮忙?

首先你的UIButton是必须隐藏的,例如你的按钮名称是btn1,然后在viewDidLoad:方法中编写此代码

- (void)viewDidLoad
{
    btn1.hidden = YES;
}
-(IBAction)start{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                             target:self
                                           selector:@selector(targetMethod:)
                                           userInfo:nil
                                            repeats:YES];
}
-(IBAction)targetMethod:(id)sender{
       btn1.hidden = NO;
}

要使用此代码,您可以在 XIB 上拖放按钮并给出名称为 BTN1 或如您所愿......

希望这个答案对你有帮助。

:)

-(void)delayA{
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(x, y, width, height); //add the parameters
[self.view addSubview:button];
}

希望对您有所帮助。 快乐编码:)

试试这个,

NSTimer *aTimer = [NSTimer timerWithTimeInterval:(5.0) target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
    NSRunLoop *runner = [NSRunLoop currentRunLoop];
    [runner addTimer:aTimer forMode: NSDefaultRunLoopMode];

并设置要在其中执行的操作

-(void)timerFired:(NSTimer *) theTimer
{
}

如果只尝试显示和隐藏按钮,请使用 alpha 属性。

// show button
[self.yourButton setAlpha: 1]
// hide button
[self.yourButton setAlpha: 0];
UIButton *btn = [UIButton alloc] initWithFrame:[Desc2 frame]];
[btn setTitle: [Desc2 text] forState:UIControlStateNormal];
[self.view addSubview:btn];

此代码片段代码创建一个 Desc2 UILabel所在的UIButton,并使用 Desc2 具有的内容设置按钮的标题。

你可以把它放在 delayA 方法中。

您可以使用

以下内容:

[NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:]

最新更新