我的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分钟不可点击?
答案 0 :(得分: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];
}
答案 1 :(得分: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];
}
答案 2 :(得分: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;
}
仅修改指南。根据需要设置重复是或否
答案 3 :(得分: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];
}
答案 4 :(得分: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]; });