限制一段时间内对button只能点按一次

来源:互联网 发布:loading windows files 编辑:程序博客网 时间:2024/06/08 18:20

#import "ViewController.h"


@implementation ViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (id)init

{

    self = [super init];

    if(self)

    {

        

    }

    return self;

}


- (void)viewDidLoad

{

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(50, 50, 100, 30);

    [btn addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(getCurrentTime) userInfo:nil repeats:YES];

    isLock = NO;

    last = 60;

    

}


-(void)btnPress:(UIButton *)btn

{

    

    if (isLock == NO)

    {

        NSLog(@"hello");

        isLock = YES;

        tempDate = [NSDate dateWithTimeIntervalSinceNow:0];

        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

        [dateFormatter setDateFormat:@"ss"];

        NSString * strDate = [dateFormatter stringFromDate:tempDate];

        last = [strDate intValue];

        isLock = YES ;

    }

}


-(void)getCurrentTime

{

    NSDate * date = [NSDate dateWithTimeIntervalSinceNow:0];

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"ss"];

    NSString * strDate = [dateFormatter stringFromDate:date];

    NSInteger nowDate = [strDate intValue];

    if(nowDate < last)

    {

        nowDate = nowDate + 60;

    }

    if(nowDate - last > 1)

    {

//        NSLog(@"nowDate = %d",nowDate);

//        NSLog(@"last = %d",last);

//        NSLog(@"nowDate - last = %d",nowDate - last);

        isLock = NO;

    }

}


@end



原创粉丝点击