限制频繁点击 button

2022-07-25  本文已影响0人  简繁之间_来去自然
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ZYFButton : UIButton
@property (nonatomic, assign) NSTimeInterval limitTimes; //限制频繁点击时间间隔 /ms
@end

NS_ASSUME_NONNULL_END


#import "ZYFButton.h"
@interface ZYFButton()

@end
@implementation ZYFButton{
    NSTimeInterval _flagTime;
}

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000; // ms
    NSLog(@"--==- time: %f---",time);
    if (!_flagTime) {
        _flagTime = time;
    }else{
        float dif = 100;  //默认限制100ms
        if (self.limitTimes) {
            dif = self.limitTimes;
        }
        NSLog(@"--==-dif: %f  time - _flagTime : %f---",dif,time - _flagTime);
        if (time - _flagTime < dif) {
            NSLog(@"--==- too fast 1  ---");
            return;
        }else{
            _flagTime = time;
            NSLog(@"--==- update _flagTime: %f---",_flagTime);
        }
    }
    [super sendAction:action to:target forEvent:event];
}
@end
上一篇下一篇

猜你喜欢

热点阅读