【问题标题】:How to properly swizzle UIScrollView with 3D touch peek pop?如何使用 3D touch peek pop 正确调整 UIScrollView?
【发布时间】:2016-03-31 00:18:51
【问题描述】:

我正在尝试调整嵌入 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");

}

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

我是不是搞错了?

【问题讨论】:

    标签: ios objective-c cocoa-touch method-swizzling swizzle


    【解决方案1】:

    我不确定滚动视图本身是否会调用“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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多