zoukankan      html  css  js  c++  java
  • 如何增加按钮的点击间隔时间

    也是写一个分类

    #import <UIKit/UIKit.h>

     

    @interface UIControl (FMGControl)

    @property (assign, nonatomic) NSTimeInterval   acceptEventInterval;

     

    @end

     

     

    ---------------下面是.m文件---------------------

    #import "UIControl+FMGControl.h"

    #import <objc/runtime.h>

    static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";

     

    @implementation UIControl (FMGControl)

    - (NSTimeInterval)acceptEventInterval

    {

        return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];

    }

     

     

    - (void)setAcceptEventInterval:(NSTimeInterval)acceptEventInterval

    {

        objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }

     

    - (BOOL)FMG_ignoreEvent

    {

        return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];

    }

     

    + (void)load

    {

        Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));

        Method b = class_getInstanceMethod(self, @selector(FMG_sendAction:to:froEvent:));

        

        method_exchangeImplementations(a, b);

    }

     

    - (void)FMG_sendAction:(SEL)action to:(id)target froEvent:(UIEvent *)event

    {

        if (self.acceptEventInterval > 0) {

            if (self.userInteractionEnabled) {

                

                [self FMG_sendAction:action to:target froEvent:event];

            }

            self.userInteractionEnabled = NO;

            

            [self performSelector:@selector(setUserInteractionEnabled:) withObject:@(YES) afterDelay:self.acceptEventInterval];

            

            

            // GCD 延迟执行 self.acceptEventInterval:为延迟时间

            __weak typeof (self) weakSelf = self;

            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.acceptEventInterval * NSEC_PER_SEC));

            

            dispatch_after(delayTime, dispatch_get_main_queue(), ^{

                weakSelf.userInteractionEnabled = YES;

            });

            

            

            

        } else {

            [self FMG_sendAction:action to:target froEvent:event];

        }

        

    }

     

    @end

     

    使用时:

      

    self.ViewButton.acceptEventInterval=5;//间隔5秒才能点击

  • 相关阅读:
    第二章 图像的显示
    c++ 使用PI
    c++函数写的都对,还是说incompatible或者not found的解决办法
    我理解的直方图均衡化
    解决360WiFi有时候手机连接不上
    c# 16进制byte转成int
    VS2010 代码突然改变字体 解决办法
    荣耀手机恢复那些“不再提示”的设置
    mfc视类中错误:IntelliSense: declaration is incompatible with。。。解决方案
    [原] Android 自定义View步骤
  • 原文地址:https://www.cnblogs.com/dashengios/p/10513343.html
Copyright © 2011-2022 走看看