# Does SetAnimationEndActionFunction work on IBSwitchControl?

**URL:** <https://iplug2.discourse.group/t/does-setanimationendactionfunction-work-on-ibswitchcontrol/656>\
**Category:** Uncategorized\
**Created:** [1 July 2022 22:18 UTC](https://iplug2.discourse.group/t/does-setanimationendactionfunction-work-on-ibswitchcontrol/656 "2022-07-01T22:18:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramdheads](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@ramdheads](https://iplug2.discourse.group/u/ramdheads)\
**Post date:** [1 July 2022 22:18 UTC](https://iplug2.discourse.group/t/does-setanimationendactionfunction-work-on-ibswitchcontrol/656/1 "2022-07-01T22:18:21Z")

</div>

I’m making a synth plugin by adapting from IPlugInstrument.

I want to keep the tempo sync function, but when I replace the IVSlideSwitchControl with a IBSwitchControl, SetAnimationEndActionFunction stops working.

In fact, I tried putting a breakpoint in the `bool sync` declaration (commented below) and even the breakpoint doesn’t work in my version.

Does SetAnimationEndActionFunction only work on some controls?

```auto
pGraphics->AttachControl(new IBKnobControl(500, 79, purpleBitmap, kParamLFORateHz), kNoTag, "LFO")->Hide(true);
    pGraphics->AttachControl(new IBKnobControl(500, 79, orangeBitmap, kParamLFORateTempo), kNoTag, "LFO")->DisablePrompt(false);
      // my version
    pGraphics->AttachControl(new IBSwitchControl(519., 189., pGraphics->LoadBitmap(BOOL_FN, 2), kParamLFORateMode), kNoTag, "LFO")->SetAnimationEndActionFunction([pGraphics](IControl* pControl) {
      bool sync = pControl->GetValue() > 0.5; //breakpoint here
      pGraphics->HideControl(kParamLFORateHz, !sync);
      pGraphics->HideControl(kParamLFORateTempo, sync);
      });
      // IPlugInstrument version
    pGraphics->AttachControl(new IVSlideSwitchControl(lfoPanel, kParamLFORateMode, "Sync", DEFAULT_STYLE, false), kNoTag, "LFO")->SetAnimationEndActionFunction([pGraphics](IControl* pControl) {
      bool sync = pControl->GetValue() > 0.5;
      pGraphics->HideControl(kParamLFORateHz, sync);
      pGraphics->HideControl(kParamLFORateTempo, !sync);
      });

```

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![olilarkin](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/olilarkin/32/2_2.png) [@olilarkin](https://iplug2.discourse.group/u/olilarkin)\
**Post date:** [2 July 2022 09:53 UTC](https://iplug2.discourse.group/t/does-setanimationendactionfunction-work-on-ibswitchcontrol/656/2 "2022-07-02T09:53:29Z")

</div>

Welcome!

IBSwitchControl doesn’t have a constructor that takes an IActionFunction, but you can set one. If a control has an IActionFunction, that function is triggered when the control is SetDirty(true). For a control that you want to animate, you can make an IActionFunction that sets an IAnimationFunction. As long as the IControl::mAnimationFunc is not null, it will get triggered at the UI refresh. In your animation function, you check the progress of the animation and call IControl::OnEndAnimation() when it is \> 1. If IControl::mEndAnimationActionFunc is set, then that IActionFunction is called.

These std::functions IActionFunction and IAnimationFunction which both have the same signature have been mainly designed for the IVControls, which have constructors that can take an action function, and the default action function is triggering the animation function. The aim being that you can create a custom animation behaviour with a tiny bit of code.

I’ll consider if IBSwitchControl needs an extra CTor, but for now, to animate an IBSwitchControl you could do something like this in your layout function…

```auto
IControl* pControl = nullptr;
  pGraphics->AttachControl(pControl = new IBSwitchControl(bounds, switchBitmap));
  pControl->SetActionFunction([](IControl* pCaller) {
    pCaller->SetValue(1.); // Set the control ON when clicked
    pCaller->SetAnimation([](IControl* pCaller){
      auto progress = pCaller->GetAnimationProgress();
      if(progress > 1.) {
        pCaller->OnEndAnimation(); // triggers IControl::mAnimationEndActionFunc
        return;
      }
    }, 500); // Animation duration of 500ms
  });
  pControl->SetAnimationEndActionFunction([](IControl* pCaller) {
    pCaller->SetValue(0.); // Set the control OFF when the animation function is cleared
  });

```

---

<div class="post-metadata">

**Author:** ![ramdheads](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@ramdheads](https://iplug2.discourse.group/u/ramdheads)\
**Post date:** [3 July 2022 10:48 UTC](https://iplug2.discourse.group/t/does-setanimationendactionfunction-work-on-ibswitchcontrol/656/3 "2022-07-03T10:48:39Z")

</div>

Thanks, it worked! I just had to remove the value setting functions because i want it to stay on or off, after clicking.

```auto
    IControl* pControl = nullptr;
    pGraphics->AttachControl(pControl = new IBSwitchControl(519., 189., pGraphics->LoadBitmap(BOOL_FN, 2), kParamLFORateMode));
    pControl->SetActionFunction([](IControl* pCaller) {
      pCaller->SetAnimation([](IControl* pCaller) {
        auto progress = pCaller->GetAnimationProgress();
        if (progress > 1.) {
          pCaller->OnEndAnimation(); // triggers IControl::mAnimationEndActionFunc
          return;
        }
        }, 500); // Animation duration of 500ms
      });
    pControl->SetAnimationEndActionFunction([pGraphics](IControl* pCaller) {
      bool sync = pCaller->GetValue();
      pGraphics->HideControl(kParamLFORateHz, sync);
      pGraphics->HideControl(kParamLFORateTempo, !sync);
      });

```
