My app has many buttons through out the application
I want to set Exclusive Touch all of them together at one time. or all views in the app
we can set individually by
[button setExclusiveTouch:YES];
But i want to set at a time for all the buttons in the application
Can we set all view exclusive touch ?
any body have any idea please suggest me.
The most elegant and actually the designed way to do this is by using the appearance
proxy, which is designed to set a defined behaviour or appearance across the board for a given UI component.
[[UIButton appearance] setExclusiveTouch:YES];
For more information: Apple Documentation - UIAppearance and NSHipster - UIAppearance
You can try this
// Not tested
for (UIView * button in [myView subviews]) {
if([button isKindOfClass:[UIButton class]])
[((UIButton *)button) setExclusiveTouch:YES];
}
If you really want to set exclusiveTouch
for ALL UIButtons + subclasses in your whole application and not just a single view, you can use method swizzling .
You use the objc runtime to override willMoveToSuperview
and set exclusive touch there. This is very reliable and i've never had any problems using this technique.
I like doing this especially for UISwitch
, because touch-handling for switches can be a bit tricky and exclusiveTouch
will help avoid bugs caused by simultaneous taps on different switches
Sample taken from the link above and changed so that exclusiveTouch
is set:
#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 insert this code for each class you want to alter.
why so difficult? Make a category
@implementation UIButton (ExclusiveTouch)
- (BOOL)isExclusiveTouch
{
return YES;
}
@end
循环浏览最顶层视图的子视图(递归),对于UIButton类型的每个对象,应用独占触摸
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.