网络编程

当前位置:永利402游戏网站-永利402com官方网站 > 网络编程 > iOS 消除button重复点击难点

iOS 消除button重复点击难点

来源:http://www.xtcsyb.com 作者:永利402游戏网站-永利402com官方网站 时间:2019-09-16 12:04

类型中日常遭逢,重复提交订单的题材。就好像从前,项目中有三个作用是抢红包,小编自身点的时候都没不符合规律,外人点就老种种提示。这种主题素材不怎么傻逼,也是必须的手艺。

#import#define NAVIITEM_INTERVAL 0.5

常见大家运用的不二等秘书技正是

-buttonClick:(UIbutton*)button{ button.enabled=No; do something(^{ button.enabled=yes;});}```###然而现在有一种新的方法,比这个更智能,更实用

@interface UIControl @property (nonatomic, assign) NSTimeInterval fy_acceptEventInterval;@property (nonatomic, assign) BOOL fy_ignore伊芙nt;@end//实现方式

@implementation UIControl static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";static const char *UIControl_ignoreEvent = "UIControl_ignoreEvent";

  • (NSTimeInterval)fy_acceptEventInterval{return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];}

  • setFy_acceptEventInterval:(NSTimeInterval)fy_acceptEventInterval{objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(fy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}

  • fy_ignoreEvent{return [objc_getAssociatedObject(self, UIControl_ignoreEvent) boolValue];}

  • setFy_ignoreEvent:fy_ignoreEvent{objc_setAssociatedObject(self, UIControl_ignoreEvent, @(fy_ignoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}

  • load{Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));Method b = class_getInstanceMethod(self, @selector(_fy_sendAction:to:forEvent:));method_exchangeImplementations;}
  • _fy_sendAction:selector to:target forEvent:event{if (self.fy_ignoreEvent) return;

    if (self.fy_acceptEventInterval > 0) {self.fy_ignoreEvent = YES;[self performSelector:@selector(setFy_ignoreEvent:) withObject:@ afterDelay:self.fy_acceptEventInterval];}

    [self _fy_sendAction:selector to:target forEvent:event];}@end

创建这样的一个category以后,就可以直接使用按钮的属性(button. fy_acceptEventInterval=2)设置间隔了。###这样做还是没有完美,我看到@茶色花的评论。如果想修改对应状态的背景图片和对应的title的话,还得继续定制。

@interfaceUIControl(Extension)//三回响应时期的光阴距离@property(nonatomic,assign)NSTimeIntervaluxy_acceptEventInterval;//是还是不是忽略事件true 忽略 flase不忽略@property(nonatomic,strong)NSNumber*uxy_ignoreEvent;@end#import"UIControl+Extension.h"

@implementationUIControl(Extension)

static constchar*UIControl_acceptEventInterval="UIControl_acceptEventInterval";

static constchar*uxy_ignoreEventKey ="uxy_ignoreEventKey";

- (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);

}

-(NSNumber*)uxy_ignoreEvent{

returnobjc_getAssociatedObject(self, uxy_ignoreEventKey);

}

-(void)setUxy_ignoreEvent:(NSNumber*)uxy_ignoreEvent{

objc_setAssociatedObject(self, uxy_ignoreEventKey, uxy_ignoreEvent, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

+ (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);

}

- (void)__uxy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent*)event{

             if(self.uxy_ignoreEvent.boolValue)return;

             if(self.uxy_acceptEventInterval >0)    {

                    self.uxy_ignoreEvent = @(YES);

                    [selfperformSelector:@selector(setUxy_ignoreEvent:) withObject:@(NO) afterDelay:self.uxy_acceptEventInterval];

}

               [self__uxy_sendAction:action to:target forEvent:event];

}

@end

在创设button的地点设置2次点击的间隔时间就能够了。如:

UIButton* btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

btn.uxy_acceptEventInterval = NAVIITEM_INTERVAL;

本文由永利402游戏网站-永利402com官方网站发布于网络编程,转载请注明出处:iOS 消除button重复点击难点

关键词:

上一篇:导航栏的UITextField点击下边视图隐蔽键盘

下一篇:没有了