Interactive Ink

Answered

Swift eraser code

Hi,

   Can you show me the eraser code in swift ? How to set the"CGPoint"、 "at" parameter, by the way ,I write the function in a button event ,does it right?


image



Best Answer

Dear Qian Bin,


currently, please find objective C code samples that show how to implement an eraser in the "Demo" sample we provide:

-First, in the MainViewController, please ensure you update the configureGestureRecognizer function adding the InputModeEraser case:

- (void)configureGestureRecognizer

{

    switch (self.editorViewController.inputMode)

    {

        case InputModeForcePen:

            self.longPressGestureRecognizer.enabled = NO;

            break;

        case InputModeForceTouch:

            self.longPressGestureRecognizer.enabled = YES;

            self.longPressGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect), @(UITouchTypeStylus)];

            break;

        case InputModeAuto:

            self.longPressGestureRecognizer.enabled = YES;

            self.longPressGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect)];

            break;

        case InputModeEraser:

            self.longPressGestureRecognizer.enabled = NO;

            break;

    }

}


-in the Frameworks\IInkUIReferenceImplementation\CapturePointHelper.m, set pointerType to IINKPointerTypeEraser when inputMode == InputModeEraser:

+ (CapturePoint)capturePointFromTouch:(UITouch *)touch inView:(UIView *)inputView inputMode:(InputMode)inputMode

{

    CGPoint point = CGPointZero;

    float f = 0.0f;

    IINKPointerType pointerType;

    if (inputMode == InputModeForcePen)

    {

        pointerType = IINKPointerTypePen;

    }

    else if (inputMode == InputModeForceTouch)

    {

        pointerType = IINKPointerTypeTouch;

    }

    else if (inputMode == InputModeEraser)

    {

        pointerType = IINKPointerTypeEraser;

    }

    else // if (_inputMode == InputMode::InputModeForceAuto)

    {

        pointerType = touch.type == UITouchTypeStylus ? IINKPointerTypePen : IINKPointerTypeTouch;

    }

    if (touch.type == UITouchTypeStylus)

    {

        point = [touch preciseLocationInView:inputView];

        f = (float)(touch.force / touch.maximumPossibleForce);

    }

    else

    {

        point = [touch locationInView:inputView];

    }

    return (CapturePoint){point, f, pointerType};

}


-In the IInkUIReferenceImplementation\CaptureTypes.h, ensure InputModeEraser is part of the InputMode enum:

typedef NS_ENUM(NSUInteger, InputMode) {

    InputModeForcePen,

    InputModeForceTouch,

    InputModeAuto,

    InputModeEraser,

};


In the Frameworks\IInkUIReferenceImplementation\EditorViewController.mm:

-Update gestureRecognizerShouldBegin as follows

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{

    return self.inputMode != InputModeForcePen && self.inputMode != InputModeEraser && self.editor.isScrollAllowed;

}

-Update setInputMode as follows:

- (void)setInputMode:(InputMode)inputMode

{

    _inputMode = inputMode;


    switch (inputMode)

    {

        case InputModeForcePen:

            self.panGestureRecognizer.enabled = NO;

            self.pinchGestureRecognizer.enabled = NO;

            break;

        case InputModeForceTouch:

            self.panGestureRecognizer.enabled = YES;

            self.panGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect), @(UITouchTypeStylus)];

            self.pinchGestureRecognizer.enabled = YES;

            self.pinchGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect), @(UITouchTypeStylus)];

            break;

        case InputModeAuto:

            self.panGestureRecognizer.enabled = YES;

            self.panGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect)];

            self.pinchGestureRecognizer.enabled = YES;

            self.pinchGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect)];

            break;

        case InputModeEraser:

            self.panGestureRecognizer.enabled = NO;

            self.pinchGestureRecognizer.enabled = NO;

            break;

    }


    self.inputView.inputMode = inputMode;

}


This way, the eraser mode should work properly.


Best regards,


Olivier




Dear Qian Bin,


currently, you should set the CGPoint as follows: editorViewController.editor.pointerMove(CGPoint(x: 0, y: 0), at: 0, force: 1, type: .eraser, pointerId: -1)


Best regards,


Olivier


1 person likes this

hi, 

    Thank you for the reply, I did as you say ,but it doesn't work.


    In your sample swift project ,I just add an "eraser" button, and write  the code  ,when I  run the project , after click the eraser button, it is still a pen. 


image


 @IBAction func btnEraser(_ sender: Any)

    {

        try? editorViewController.editor.pointerMove(CGPoint(x: 0, y: 0), at: 0, force: 1, type: .eraser, pointerId: -1)

    }

image


Dear Qian Bin,


Currently, the previous answer was just to explain how you can initialize the CGPoint, following your question...


To implement a full eraser, you should catch the pointer events and add these using the pointerMove, pointerUp and  pointerDown functions (see API documentation: http://cocoadocs.org/docsets/MyScriptInteractiveInk-Runtime/1.0.0/Classes/IINKEditor.html#//api/name/pointerDown:at:force:type:pointerId:error:)... These 3 functions can be called similarly to the above pointerMove example.


Best regards,


Olivier


Hi @Olivier,
Thank you for the explain!
Just now I have spend hours try to do as your say ,and search the Forum for relatvie information, but there is little swift code ,I can't get the idea in below topics

Could you explain further how to catch the pointer events in SWIFT, and then how to make the "earser" work?


best regards

" catch the pointer events " does it mean I should call the touchesMoved event?


 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    }

 

Answer

Dear Qian Bin,


currently, please find objective C code samples that show how to implement an eraser in the "Demo" sample we provide:

-First, in the MainViewController, please ensure you update the configureGestureRecognizer function adding the InputModeEraser case:

- (void)configureGestureRecognizer

{

    switch (self.editorViewController.inputMode)

    {

        case InputModeForcePen:

            self.longPressGestureRecognizer.enabled = NO;

            break;

        case InputModeForceTouch:

            self.longPressGestureRecognizer.enabled = YES;

            self.longPressGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect), @(UITouchTypeStylus)];

            break;

        case InputModeAuto:

            self.longPressGestureRecognizer.enabled = YES;

            self.longPressGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect)];

            break;

        case InputModeEraser:

            self.longPressGestureRecognizer.enabled = NO;

            break;

    }

}


-in the Frameworks\IInkUIReferenceImplementation\CapturePointHelper.m, set pointerType to IINKPointerTypeEraser when inputMode == InputModeEraser:

+ (CapturePoint)capturePointFromTouch:(UITouch *)touch inView:(UIView *)inputView inputMode:(InputMode)inputMode

{

    CGPoint point = CGPointZero;

    float f = 0.0f;

    IINKPointerType pointerType;

    if (inputMode == InputModeForcePen)

    {

        pointerType = IINKPointerTypePen;

    }

    else if (inputMode == InputModeForceTouch)

    {

        pointerType = IINKPointerTypeTouch;

    }

    else if (inputMode == InputModeEraser)

    {

        pointerType = IINKPointerTypeEraser;

    }

    else // if (_inputMode == InputMode::InputModeForceAuto)

    {

        pointerType = touch.type == UITouchTypeStylus ? IINKPointerTypePen : IINKPointerTypeTouch;

    }

    if (touch.type == UITouchTypeStylus)

    {

        point = [touch preciseLocationInView:inputView];

        f = (float)(touch.force / touch.maximumPossibleForce);

    }

    else

    {

        point = [touch locationInView:inputView];

    }

    return (CapturePoint){point, f, pointerType};

}


-In the IInkUIReferenceImplementation\CaptureTypes.h, ensure InputModeEraser is part of the InputMode enum:

typedef NS_ENUM(NSUInteger, InputMode) {

    InputModeForcePen,

    InputModeForceTouch,

    InputModeAuto,

    InputModeEraser,

};


In the Frameworks\IInkUIReferenceImplementation\EditorViewController.mm:

-Update gestureRecognizerShouldBegin as follows

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{

    return self.inputMode != InputModeForcePen && self.inputMode != InputModeEraser && self.editor.isScrollAllowed;

}

-Update setInputMode as follows:

- (void)setInputMode:(InputMode)inputMode

{

    _inputMode = inputMode;


    switch (inputMode)

    {

        case InputModeForcePen:

            self.panGestureRecognizer.enabled = NO;

            self.pinchGestureRecognizer.enabled = NO;

            break;

        case InputModeForceTouch:

            self.panGestureRecognizer.enabled = YES;

            self.panGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect), @(UITouchTypeStylus)];

            self.pinchGestureRecognizer.enabled = YES;

            self.pinchGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect), @(UITouchTypeStylus)];

            break;

        case InputModeAuto:

            self.panGestureRecognizer.enabled = YES;

            self.panGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect)];

            self.pinchGestureRecognizer.enabled = YES;

            self.pinchGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeDirect)];

            break;

        case InputModeEraser:

            self.panGestureRecognizer.enabled = NO;

            self.pinchGestureRecognizer.enabled = NO;

            break;

    }


    self.inputView.inputMode = inputMode;

}


This way, the eraser mode should work properly.


Best regards,


Olivier



Dear Olivier, 

            Thanks a lot ! It works perfectly!


Best regards