1

我的 iOS 应用中有按钮:

    _Button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *shareIMG = [UIImage imageNamed:@"button.png"];
    [_Button setBackgroundImage:shareIMG forState:UIControlStateNormal];
    [_Button setBackgroundImage:[UIImage imageNamed:@"button_active.png"] forState:UIControlStateHighlighted];
    [_Button addSubview:titleLabel];

    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 3, shareIMG.size.width, shareIMG.size.height)];
    [titleLabel setTextAlignment:UITextAlignmentCenter];
    [titleLabel setText:@"Button";
    [_Button addSubview:titleLabel];
    [titleLabel release];
    [_Button setFrame:CGRectMake(2 * self.sendPushButton.frame.origin.x + self.sendPushButton.frame.size.width , 380 - liteIndent1 - liteIndent2 + iphone5Fix, shareIMG.size.width, shareIMG.size.height)];
    [self addSubview:_Button];

您能帮我如何使此按钮处于非活动状态(button_non_active.png)并且在点击后 10 分钟内不可点击吗?

4

5 回答 5

5
// disable button
[_Button setEnabled:NO];

// run a selector after 10 minutes
[_Button performSelector:@selector(onEnableButton:) withObject:_Button afterDelay:(10.0 * 60.0)]


- (void) onEnableButton:(UIButton *)sender
{
 [sender setEnabled:YES];
}
于 2013-01-15T13:52:58.493 回答
0

在同一个类中创建一个方法来重新启用它:

-(void)enableButton:(UIButton *)button {
    [button setBackgroundImage:[UIImage imageNamed:@"button_active.png"] forState:UIControlStateHighlighted];
    button.enabled = YES;
}

以及按下时将触发的选择器(您的代码段中似​​乎缺少这个。用 设置它[_Button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

-(void)buttonPressed:(UIButton *)button {
    //do actual action here
    [button setBackgroundImage:[UIImage imageNamed:@"button_non_active.png"] forState: UIControlStateHighlighted];
    button.enabled = NO;
    [self performSelector: @selector(enableButton:) withObject: button afterDelay: 600.0];
}
于 2013-01-15T13:52:20.557 回答
0

使用`

 btn.enabled=NO;`

  NSTimer  *   notificationTimer = [NSTimer scheduledTimerWithTimeInterval:10*60.0 target:self selector:@selector(enable) userInfo:nil repeats:No];

    -(void)enable
    {
    btn.enabled=YES;
    }

修改它只是一个指导。根据需要设置重复是或否

于 2013-01-15T13:52:23.797 回答
0

单击按钮时,您必须调用一个函数。

- (void)buttonClickedStartTimer
{
    _Button.userInteractionEnabled = FALSE;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(stopTimer) userInfo:nil repeats:YES];

}

- (void)stopTimer
{
     _Button.userInteractionEnabled = TRUE;
    [timer invalidate];
    [timer release];
}
于 2013-01-15T13:53:22.500 回答
0
  _Button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_Button setImage:[UIImage imageNamed:@"button_active.png"] forState:UIControlStateNormal];
    [_Button setImage:[UIImage imageNamed:@"button_non_active.png"] forState:UIControlStateDisabled];
    [_Button addSubview:titleLabel];
   [_Button setEnabled:NO];

    int64_t delayInSeconds = 60.0 * 10;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
       [_Button setEnabled:YES];
    });

于 2013-01-15T13:55:30.193 回答