iOS: setting Exclusive Touch to all buttons in a view

后端 未结 8 450
灰色年华
灰色年华 2021-01-03 16:17

My app has many buttons in a Window and I want to set Exclusive Touch all of them together. Do you have any suggestion about this? Thanks

相关标签:
8条回答
  • 2021-01-03 17:07

    If these buttons are all in the same view, you can loop through the view's subviews, test for whether the particular subview is a button (or test for a tag if you have one set) and set exclusiveTouch on each.

    0 讨论(0)
  • 2021-01-03 17:08

    There is a way to set exclusive touch to all buttons in your app, may be helpful.

    #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 don't understand this, you can refer to this and this.

    0 讨论(0)
  • 2021-01-03 17:08
    -(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 at viewDidAppear

    0 讨论(0)
  • 2021-01-03 17:08

    If you want to set exclusiveTouch for ALL UIButtons in your whole application method swizzling will be the perfect solution for you.

    This answer explains the way very well : https://stackoverflow.com/a/24534814/976246 , and it works perfectly for me.

    Also go through this article to know how this (http://nshipster.com/method-swizzling/) tecknique can be used for various purposes.

    0 讨论(0)
  • 2021-01-03 17:13

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

    If you have all the buttons in an array (e.g. they're all connected to the same IBOutletCollection) you can use key value coding to set the exclusiveTouch property of the array:

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

    NSArray will then invoke the same method on every item in the array.

    0 讨论(0)
  • 2021-01-03 17:13

    I just found an 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];
        }
    }
    

    Source

    0 讨论(0)
提交回复
热议问题