Http://www.cocoachina.com/ios/20150911/13260.html
Uxyheaven authorized this site reproduced.
What is this problem
Our button is a one-click response, even if the frequent click will not be a problem, but some scenarios will be the problem.
is usually how to solve
We usually set this button to not click when the button is clicked. Wait for 0.xS delay after setting it back; Or the settings can be clicked at the end of the operation.
123456 |
- (IBAction)clickBtn1:(UIbutton *)sender { sender.enabled = NO; doSomething sender.enabled = YES; } |
If there are different styles of buttons in different states, it is not sufficient to use enabled. Additional variables are added to record the status.
1234567 |
- (IBAction)clickBtn1:(UIbutton *)sender { if (doingSomeThing) return ; doingSomeThing = YES; doSomething doingSomeThing = NO; } |
An example of this is the direct prohibition of clicks directly within the cycle of responding to events. If you want to disable repeated clicks within 1 seconds, you have to use PerformSelector:withObject:afterDelay:
What's a pretty solution?
Having a repeating code snippet is a common denominator that can be abstracted.
We can add an attribute repeat click Interval to the button, by setting this property to control the time interval to accept the Click event again.
123456789101112 |
@interface UIControl (XY)
@property (nonatomic, assign) NSTimeInterval uxy_acceptEventInterval;
// 可以用这个给重复点击加间隔
@end
static const char *UIControl_acceptEventInterval =
"UIControl_acceptEventInterval"
;
- (NSTimeInterval)uxy_acceptEventInterval
{
return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}
- (void)setUxy_acceptEventInterval:(NSTimeInterval)uxy_acceptEventInterval
{
objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(uxy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
|
When the app launches, we hook all the button's event
12345678 |
@implementation UIControl (XY) + (void)load { Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:)); Method b = class_getInstanceMethod(self, @selector(__uxy_sendAction:to:forEvent:)); method_exchangeImplementations(a, b); } @end |
In our Click event, filter the Click event
12345678910 |
- (void)__uxy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
if
(self.uxy_ignoreEvent)
return
;
if
(self.uxy_acceptEventInterval > 0)
{
self.uxy_ignoreEvent = YES;
[self performSelector:@selector(setUxy_ignoreEvent:) withObject:@(NO) afterDelay:self.uxy_acceptEventInterval];
}
[self __uxy_sendAction:action to:target forEvent:event];
}
|
Actually, that's what it looks like.
123 |
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [tempBtn addTarget:self action:@selector(clickWithInterval:) forControlEvents:UIControlEventTouchUpInside]; tempBtn.uxy_acceptEventInterval = 0.5; |
This is the end of the article. Although it is not recommended to use runtime in a wide range, small-scale operation can solve many small problems.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS tips: Use runtime to solve UIButton repeated click problems