You need additional variables: previousButtonCondition, encoderFunction.
-
read condition of button
-
if button is pressed and previous condition is depressed, toggle encoder function
-
write condition of button to previous condition variable
-
do your "if" loop, but not with reading button, instead use encoder function.
Do you understand?