NativeScript / capacitor

NativeScript for Capacitor

Home Page:https://capacitor.nativescript.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`Interaction` will not work after about 15 seconds

iamcco opened this issue · comments

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

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

Wow, it works great 💯 ! Thanks for the tips! 👍