#iOS dev disables multiple button clicks causing accidental touch

This is a question that many developers don’t consider when developing iOS projects, including me —— what if there are multiple buttons or touch events going on simultaneously in one interface?

It may not be easy to understand just from the literal, but let me give you an example that is easier to understand. On the home page of an application, there are two buttons, and clicking each button will trigger a click event. There is nothing wrong with this, just like clicking the first button to jump to the first sub-interface, and clicking the second button to jump to the second sub-interface. But as we do mobile development, do we ever have such awkward situations? Our fingers are thick, or more than one finger colleagues operation, occasionally we will accidentally touch, resulting in an embarrassing scene —— two buttons clicked at the same time, so the result is, will flash two jump page, jump the first sub-interface and jump the second sub-interface will happen. This is clearly not what we want. So how do you block it?

1. We may think of the way to disable button clicking. If button 1 is clicked, it will immediately prohibit button 2’s clicking, and open button 2’s clicking after the event ends. Not completely, if the code to disable button clicking has already been implemented. We try again and again and still find false contact. This works in principle, but when you actually click, you’ll find that the implementation of the forbidden button and the sequence of events in which the button is clicked are not completely guaranteed… If the two clicks are slightly staggered, it’s ok, but if the two clicks are very close, it can be a problem. If this is the state, no problem

Of course if you say you can defer execution, make sure you defer every execution and make judgments… Of course such a complex operation is certainly possible, but it is obviously too complicated. So there’s nothing we can do? Or let’s look at systems and see if there are other ways we can do it.

2. Obviously, there is an approach in the iOS development framework.

So we’ll just write one more line of code in development, at creation time. Similar to [Button setExclusiveTouch:YES]; Or button.exclusiveTouch = YES; Yes, you have tried the result, it is exactly what we want, we will not have that kind of embarrassing non-logical error happen again, the boss who does not understand the code will not say you will not develop your application at least because of this problem (think back to me when I just developed…..). .

3. Given such a good method, can we use it? Obviously, we don’t have a lot of simultaneous events in development, and we don’t have a lot of simultaneous events (except games…). We’re all familiar with the runtime mechanism in iOS development. Can we use this to write this line of code directly? I have an idea. All UIViews added to the interface obviously need this method,addSubview:

So I thought it would be nice to create a category


#import "UIView+Extension.h"
#import <objc/runtime.h>

@implementation UIView (Extension)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL systemSel = @selector(addSubview:);
        SEL swizzSel = @selector(myAddSubview:);
        Method systemMethod = class_getInstanceMethod([self class], systemSel);
        Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
        BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
        if (isAdd) {
            class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
        }else{ method_exchangeImplementations(systemMethod, swizzMethod); }}); } - (void)myAddSubview:(UIView *)view{if ([view respondsToSelector:@selector(setExclusiveTouch:)]) {
        [view setExclusiveTouch:YES];
    }
    [self myAddSubview:view];
}

@end
Copy the code

Use the Runtime mechanism to set the ‘exclusiveTouch’ property of a view before it is added to the view by the owner.

Obviously, we don’t have to worry about this anymore when we’re writing code, all buttons, all inheritance, all things from UIView are going to be implemented by default, so let’s go back and say, all of our controls that trigger clicks, are there any controls that don’t inherit from UIView?

As a result, we perfectly disabled the awkwardness of clicking multiple buttons at the same time.

3. Of course, the above logic is just one of my ideas, we can also use the way of traversal oh we replace VC viewDidAppear: method

#import "UIViewController+Extension.h"

@implementation UIViewController (Extension)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        
        SEL originalSelector = @selector(viewDidAppear:);
        SEL swizzledSelector = @selector(myViewDidAppear:);
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (success) {
            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else{ method_exchangeImplementations(originalMethod, swizzledMethod); }}); } -(void)myViewDidAppear:(BOOL)animated{for (UIView * subview in self.view.subviews) {
		if ([subview respondsToSelector:@selector(setExclusiveTouch:)]) {
	        [subview setExclusiveTouch:YES];
	    }
    }
    [self myViewDidAppear:animated];
}

@end

Copy the code

I’m going to iterate through the subviews in viewDidAppear: as the viewController comes to the screen, and of course, is there any subviews in the subviews? Does this need to be traversed as well? It’s up to the developers themselves to decide whether to go deep or wide… These are beyond the scope of this blog…

Welcome to my series of blogs, just starting to write… Series: iOS development – preface + outline blog.csdn.net/spicyShrimp…