How to make PKInkingTool width constant?

We want to use new iOS 14 PencilKit vector capabilities in our app.

However, our current drawing component is always fixed-width. If we use dynamic brush width, like PencilKit has, it will look different on our other platforms.

Is there a way to make PKInkingTool having fixed width?

I tried swizzling methods like defaultWidthForInkType:, but my swizzled methods are never called.

Code Block objc-c
@implementation PKInkingTool (Tracking)
+ (void)load {
    [super load];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        const char *className = [NSStringFromClass(self) UTF8String];
        Class class = objc_getMetaClass(className);
        SEL originalSelector = @selector(defaultWidthForInkType:);
        SEL swizzledSelector = @selector(ed_defaultWidthForInkType:);
        Method originalMethod = class_getClassMethod(class, originalSelector);
        Method swizzledMethod = class_getClassMethod(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);
        }
}
+ (CGFloat)ed_defaultWidthForInkType:(PKInkType)inkType {
    return 15.0;
}
@end


Replies

One way I found is to swizzle UITouch force and timestamp methods.

#import <objc/runtime.h>

@implementation UITouch (UITouch_Overrides)

+ (void)load {
    [super load];

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleForce];
        [self swizzleTimestamp];
    });
}

+ (void)swizzleForce {
    [self swizzle:@selector(force) with:@selector(ed_force)];
}

+ (void)swizzleTimestamp {
    [self swizzle:@selector(timestamp) with:@selector(ed_timestamp)];
}

+ (void)swizzle:(SEL)originalSelector with:(SEL)swizzledSelector {
    Class class = [self class];

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

}

- (CGFloat)ed_force {
    return 1.0;
}

- (NSTimeInterval)ed_timestamp {
    return 0;
}

@end