美文网首页
限制频繁点击 button

限制频繁点击 button

作者: 简繁之间_来去自然 | 来源:发表于2022-07-25 15:38 被阅读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
    

    相关文章

      网友评论

          本文标题:限制频繁点击 button

          本文链接:https://www.haomeiwen.com/subject/kjigirtx.html