IOS: setting Exclusive Touch for all buttons in the view

My application has many buttons in the window, and I want to install Exclusive Touch all together. Do you have any suggestions on this? Thanks

0
source share
8 answers

There is a way to set an exclusive touch to all buttons in your application, it can be useful.

#import </usr/include/objc/objc-class.h> static IMP gOringinalWillMoveToSuperview = nil; static id newMoveToSuperviewPlusSettingExclusiveTouch(id self,SEL selector,...) { va_list arg_list; va_start( arg_list,selector); gOringinalWillMoveToSuperview(self,selector,arg_list); [self setExclusiveTouch:YES]; return nil; } -(void)addSettingExclusiveTouchToAllUIViewMethodWillMoveToSuperview { gOringinalWillMoveToSuperview = class_getMethodImplementation([UIButton class], @selector(willMoveToSuperview:)); class_replaceMethod([UIButton class], @selector(willMoveToSuperview:), &newMoveToSuperviewPlusSettingExclusiveTouch, " v@ :"); } 

If you do not understand this, you can refer to this and this .

+5
source

Are you just looking for an easy way to install them all at once?

If you have all the buttons in the array (for example, they are all associated with the same IBOutletCollection), you can use the encoding of the key values ​​to set the exclusiveTouch property of the array:

 [buttonArray setValue:[NSNumber numberWithBool:YES] forKey:@"exclusiveTouch"]; 

NSArray will then call the same method for each element of the array.

+1
source

If these buttons are in the same view, you can scroll through the view in the form of a view, check whether a particular view (or a tag for a tag, if you have one set), and set an exclusive copy for each of them.

+1
source

I just found the answer for this:

 #pragma mark Set Buttons Exclusive Touch Yes -(void)setExclusiveTouchForButtons:(UIView *)myView { for (UIView * button in [myView subviews]) { if([button isKindOfClass:[UIButton class]]) [((UIButton *)button) setExclusiveTouch:YES]; } } 

A source

+1
source
 -(void)setExclusiveTouchForButtons:(UIView *)myView { for (UIView * v in [myView subviews]) { if([v isKindOfClass:[UIButton class]]) [((UIButton *)v) setExclusiveTouch:YES]; else if ([v isKindOfClass:[UIView class]]){ [self setExclusiveTouchForButtons:v]; } } } 

then call this function in viewDidAppear

+1
source

If you want to install an exclusive offer for ALL UIButtons throughout the application, the swizzling method will be the perfect solution for you.

This answer explains very well: fooobar.com/questions/950270 / ... and it works great for me.

Also read this article to find out how this (http://nshipster.com/method-swizzling/) tecknique can be used for various purposes.

+1
source

If you add buttons pragmatically, send a message to the [button setExclusiveTouch:YES]; for each button before adding it to the super view. If you use xib, you must send the same message to the button in viewDidLoad or in loadView.

0
source

Here is the code in swift that will set an exclusive touch to all buttons in the viewcontroller view

 for button in self.view.subviews { if(button.isKindOfClass(UIButton)){ (button as! UIButton).exclusiveTouch = true } } 
0
source

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


All Articles