本文为博主手写总结性文章,如若涉及版权问题,请与博主联系。
关于暴力点击问题解决涉及的主要代码:
@property BOOL violentClick;
self.violentClick = true;
if (self.violentClick == true) {
self.violentClick = false;
[self performSelector:@selector(violentClickAction) withObject:nil afterDelay:0.5f];
//相关操作
}
- (void)violentClickAction{
self.violentClick = true;
}
简单的说明一下思路问题:
先定义一个BOOL型的变量,然后设置变量初始化值为true,接下来在点击操作执行之后的操作中做判断变量是否为true,如果为true说明是第一次点击,并且在第一次点击之后,变量改为false,设置延迟,再将变量改回为true。这样就保证了延迟期间内多次点击只执行一次。
下面举个例子:
#import "ViewController.h"
@interface ViewController ()
//创建BOOL类型变量
@property BOOL violentClick;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化变量
self.violentClick = true;
//创建一个按钮
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 150, 50)];
[btn setTitle:@"我是一个按钮" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor blueColor]];
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)btnAction{
//判断变量是否为第一次点击
if (self.violentClick) {
self.violentClick = false;
[self performSelector:@selector(violentClickAction) withObject:nil afterDelay:5.0f];
NSLog(@"按钮被点击了");
}
}
-(void)violentClickAction{
self.violentClick = true;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
PS:因为实在不知道该如何展示效果,所以就不展示了。