Post not yet marked as solved
Post marked as unsolved with 1 replies, 1,001 views
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.
@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