Here’s how I would do it with Mouse input. You can adapt this pretty easily to your touch world. The secret is keeping track of the current rotation and clamping the current rotation plus proposed rotation.
Your code worked perfectly but I don’t understand this code. Why I have to take old rotation, then subtract from new rotation and then put new rotation value in old rotation. But thank you very much for your help.