Ios 单击按钮快速按UIViewController两次
示例: 我在当前视图中有两个按钮,当我单击该按钮时,它将在当前Ios 单击按钮快速按UIViewController两次,ios,iphone,swift,uinavigationcontroller,pushviewcontroller,Ios,Iphone,Swift,Uinavigationcontroller,Pushviewcontroller,示例: 我在当前视图中有两个按钮,当我单击该按钮时,它将在当前UINavgitionController中按下一个UIViewController 这是我的代码: dispatch_async(dispatch_get_main_queue(), { () -> Void in self.navigationController?.pushViewController(pvc, animated: true) }) 但我发现了一只虫子。当我快速点击按钮时,UIViewContro
UINavgitionController
中按下一个UIViewController
这是我的代码:
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.navigationController?.pushViewController(pvc, animated: true)
})
但我发现了一只虫子。当我快速点击按钮时,UIViewController
按下两次,为什么会发生这种情况
PS:现在,我已经在许多
UIViewController
s中使用了此代码,超过200次。我也遇到了同样的问题,我通过创建UIButton类别来解决它,该类别只会进行独占触摸
创建UIButton的类别
#import <objc/runtime.h> //Please import
@implementation UIButton (ExclusiveTouch)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(willMoveToSuperview:);
SEL swizzledSelector = @selector(inc_willMoveToSuperview:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)inc_willMoveToSuperview:(UIView *)newSuperview
{
// This is correct and does not cause an infinite loop!
// See the link for an explanation
[self inc_willMoveToSuperview:newSuperview];
[self setExclusiveTouch:YES];
}
#导入//请导入
@实现UIButton(排他性Touch)
+(空)荷载
{
静态调度一次;
一次发送(一次发送)^{
Class=[self Class];
SEL originalSelector=@选择器(willMoveToSuperview:);
选择开关选择器=@selector(inc_willMoveToSuperview:);
方法originalMethod=class\u getInstanceMethod(class,originalSelector);
方法swizzledMethod=class_getInstanceMethod(class,swizzledSelector);
布尔-迪达德法=
类\添加方法(类,
原选举人,
方法_getImplementation(swizzledMethod),
方法_getTypeEncoding(swizzledMethod));
if(didAddMethod){
类替换方法(类,
瑞士选民,
方法_getImplementation(原始方法),
方法_getTypeEncoding(原始方法));
}否则{
方法交换实施(原始方法、swizzledMethod);
}
});
}
-(无效)公司将移动到Superview:(UIView*)newSuperview
{
//这是正确的,不会导致无限循环!
//有关说明,请参见链接
[self inc_willMoveToSuperview:newSuperview];
[自我设置排他性接触:是];
}
使用您的代码,它就会发生<代码>调度异步(调度获取主队列()不会立即执行。您可以在单击按钮后禁用它。@anhtu是对的。希望您理解异步和同步调用的概念。现在,当您两次点击它时,它会向队列发送两次命令。与异步调用一样,它们中的每一个在运行时都会被调用,从而导致pu点击VC两次。如果你点击按钮两次,它将导航两次。因此,在控件导航到屏幕之前,你已经放置了加载程序或活动指示器……你能告诉我如何使用它吗?@ROCKING XCode