`Interaction` will not work after about 15 seconds
iamcco opened this issue · comments
年糕小豆汤 commented
My code defined in src/nativescript/utils
After call native.enablePencilTap()
I can see log when tap the apple pencil, but about 15 seoncds it does not work any more. I have to recall native.enablePencilTap()
to make it work again.
It's that normal? Can I find a way to always to interaction with apple pencil?
import { iosRootViewController } from '@nativescript/capacitor/bridge';
let showLog: () => void;
@NativeClass()
class UIPencilInteractionDelegateClass extends NSObject {
public static ObjCProtocols = [UIPencilInteractionDelegate];
pencilInteractionDidTap(_interaction: UIPencilInteraction) {
console.log(UIPencilInteraction.preferredTapAction);
showLog();
}
}
native.enablePencilTap = (cb: () => void) => {
if (native.isAndroid) {
return;
}
showLog = cb;
const uiPencilR = UIPencilInteraction.alloc().init();
uiPencilR.delegate = UIPencilInteractionDelegateClass.new();
iosRootViewController().view.addInteraction(uiPencilR);
};
Nathan Walker commented
@iamcco Most likely the controller and delegate reference is getting garbage collected - we mention this in the best practices note here for iOS:
https://docs.nativescript.org/best-practices/ios-tips.html
Try something like this:
let uiPencilR;
let uiPencilRDelegate;
native.enablePencilTap = (cb: () => void) => {
if (native.isAndroid) {
return;
}
showLog = cb;
if (!uiPencilR) {
uiPencilR = UIPencilInteraction.alloc().init();
}
if (!uiPencilRDelegate) {
uiPencilRDelegate = UIPencilInteractionDelegateClass.new();
}
uiPencilR.delegate = uiPencilRDelegate;
iosRootViewController().view.addInteraction(uiPencilR);
};
年糕小豆汤 commented
Wow, it works great 💯 ! Thanks for the tips! 👍