UIDatePicker inside UIScrollView with pages

后端 未结 3 1027
执念已碎
执念已碎 2021-01-03 05:19

I have a UIScrollView with 2 pages, and I can scroll horizontally between them. However, on one of my pages, I have a UIDatePicker, and the scroll view is intercepting the

3条回答
  •  天涯浪人
    2021-01-03 06:13

    Awesome help Sam! I used that to create a simple category that swizzles the method (because I was doing this in a UITableViewController and thus would have had to do some really messy stuff to subclass the scroll view).

    #import 
    
    @interface UIScrollView (withControls)
    
    + (void) swizzle;
    
    @end
    

    And the main code:

    #import 
    #import "UIScrollView+withControls.h"
    
    #define kUIViewBackgroundImageTag 6183746
    static BOOL swizzled = NO;
    
    @implementation UIScrollView (withControls)
    
    + (void)swizzleSelector:(SEL)orig ofClass:(Class)c withSelector:(SEL)new;
    {
        Method origMethod = class_getInstanceMethod(c, orig);
        Method newMethod = class_getInstanceMethod(c, new);
    
        if (class_addMethod(c, orig, method_getImplementation(newMethod),
                            method_getTypeEncoding(newMethod))) {
            class_replaceMethod(c, new, method_getImplementation(origMethod),
                                method_getTypeEncoding(origMethod));
        } else {
            method_exchangeImplementations(origMethod, newMethod);
        }
    }
    
    + (void) swizzle {
        @synchronized(self) {
            if (!swizzled) {
    
                [UIScrollView swizzleSelector:@selector(hitTest:withEvent:)
                                      ofClass:[UIScrollView class]
                                 withSelector:@selector(swizzledHitTest:withEvent:)];
                swizzled = YES;
            }
        }
    }
    
    - (UIView*)swizzledHitTest:(CGPoint)point withEvent:(UIEvent *)event {
        UIView* result = [self swizzledHitTest:point withEvent:event]; // actually calling the original hitTest method
    
        if ([result.superview isKindOfClass:[UIPickerView class]]) {
            self.canCancelContentTouches = NO;  
            self.delaysContentTouches = NO;
        } else {
            self.canCancelContentTouches = YES; // (or restore bool from prev value if needed)
            self.delaysContentTouches = YES;    // (same as above)
        }
        return result;
    }
    
    @end
    

    Then, in my viewDidLoad method, I just called

    [UIScrollView swizzle];
    

提交回复
热议问题