1

我正在尝试调整嵌入 3D Touch 的 peek and pop 预览的 UIScrollView。(我知道它是通过 Reveal 应用程序的 UIScrollView。)

我想知道用户何时在此滚动视图/3D 触摸预览上移动手指。

我尝试按如下方式调整它:

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        SEL originalSelector = @selector(setContentOffset:);
        SEL swizzledSelector = @selector(xxx_setContentOffset:);

        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);
        }
    });
}

#pragma mark - Method Swizzling

- (void)xxx_setContentOffset:(CGPoint)offset {
    [self xxx_setContentOffset:offset];

    NSLog(@"yes");

}

但是当我的手指在屏幕上滑动应该有数百个呼叫时,它只会呼叫一次或两次“是”。

我错了吗?

4

1 回答 1

0

我不确定滚动视图本身是否调用“setter”,因此您不一定会收到回调。

我在 contentOffset 属性上通过 KVO 在滚动视图中创建了一个“浮动”视图,它按预期调用。您可能可以在您的情况下使用相同的机制:

        /**
         *  We are using KVO to offset the background image view
        */

        [self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"contentOffset"]) {
        _bgImageView.frame = CGRectMake(self.contentOffset.x,0, self.frame.size.width, self.frame.size.height);
    }
}

但作为参考,我像这样混为一谈:

+(void)load {
    NSArray *selectors = @[NSStringFromSelector(@selector(setText:)),...];

    for (NSString *name in selectors) {
        SEL originalSelector = NSSelectorFromString(name);
        SEL swizzledSelector = NSSelectorFromString([NSString stringWithFormat:@"iio_swizzled_%@",name]);
        Method originalMethod = class_getInstanceMethod(self, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
        class_addMethod(self,
                        originalSelector,
                        class_getMethodImplementation(self, originalSelector),
                        method_getTypeEncoding(originalMethod));
        class_addMethod(self,
                        swizzledSelector,
                        class_getMethodImplementation(self, swizzledSelector),
                        method_getTypeEncoding(swizzledMethod));
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }

}

-(void)iio_swizzled_setText:(NSString *)text {
   ... do stuff
   // and if needed to call the original 
  [self iio_swizzled_setText:text];
}
于 2016-04-11T11:04:05.710 回答