Goal c, how to set an exclusive touch for all UIbuttons for the entire application

My application has many buttons through the application

I want to install Exclusive Touch all together at the same time. or all views in the application

we can install individually

[button setExclusiveTouch:YES]; 

But I want to install all the buttons in the application at a time

Can we install all the exclusive touch screens?

Any body has any ideas, please suggest me.

+3
source share
5 answers

You can try this

 // Not tested for (UIView * button in [myView subviews]) { if([button isKindOfClass:[UIButton class]]) [((UIButton *)button) setExclusiveTouch:YES]; } 
+8
source

The most elegant and actually developed way to do this is through the appearance proxy, which is designed to set a specific behavior or appearance throughout the board for this user interface component.

 [[UIButton appearance] setExclusiveTouch:YES]; 

For more information: Apple Documentation - UIAppearance and NSHipster - UIAppearance

+8
source

If you really want to set exclusiveTouch for ALL UIButtons + subclasses in your application, and not just in one view, you can use the swizzling method.

You use the objc runtime to override willMoveToSuperview and set the exclusive touch there. It is very reliable and I have never had a problem using this technique.

I like to do this specifically for UISwitch , because touch processing for switches can be a bit complicated, and exclusiveTouch will help avoid errors caused by simultaneous taps on different switches.

Sample taken from the link above and modified so that exclusiveTouch installed:

 #import <objc/runtime.h> @implementation UIButton (INCButton) + (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]; } @end 

Create a category and paste this code for each class that you want to change.

+6
source

why is it so difficult? Make Category

 @implementation UIButton (ExclusiveTouch) - (BOOL)isExclusiveTouch { return YES; } @end 
+6
source

The cycle through the subselects (recursively) of the topmost view and for each object of the UIButton type an exclusive touch is applied

0
source

Source: https://habr.com/ru/post/950270/


All Articles