In comment someone asked if I was victim-blaming in this answer. No, it's not victim blaming because I never blamed the victim for anything. What I'm saying is that 1) two wrongs don't make a right and 2) the other person might not actually be a bully and you won't know until you handle the situation appropriately. Maybe OP did handle it appropriately: maybe, maybe not, we don't know exactly how it happened.
If OP did act appropriately in a manner I've pointed out, then that just means they're already partway into the steps. If your complaint is "OP's already started that and it's not working well," well then I have covered that too in the answer below.
How to not act
However, they are my friends so I feel bad if I tell them off about it or legitimately freak out. I am not someone who can handle a serious confrontation without panicking
This is your social issue right here. You should feel bad about telling them off or freaking out since there is no need for that except as a last resort after trying more civil things multiple times. Well, at least originally, though now you say you've mentioned it to them, but do you remember precisely how you mentioned it?
If you consider someone your friend, you should treat them nicely. If you said any of the things mentioned in Elmy's answer, such as "Stop bullying me (in a loud and assertive voice)" or "Get your grabby hands off me!", as a first reaction without first making it over-abundantly obvious how you feel and without giving me the slack of a couple of accidental violations (ie: "Oops sorry, it's sort of become habit now. But I respect you and will try not to do it again.") then I would consider you to be the mean one. It is ok only as a last resort.
You also need to be careful that you make your actual intention obvious. If I was your friend and you told me today "Please don't touch my hair" when it's a thing I do every day, I might do it again tomorrow since you weren't clear that I need to never touch your hair. Then the next day, if you "told me off" about it I would assume you were just grumpy that day and being a jerk. In fact, I might yell back at you and tell you off for your outburst.
In fact, it is possible that your friends might push more just because you don't respond appropriately. I used to be that way too.
Years ago at a public event I stood next to a tree to watch a performance. Some people came after me, sat down about 5 meters behind me, then they threw some popcorn at me and shouted rudely "Sit down in front!" That is a completely inappropriate first request, so I just yelled back at them "Shut up in back!" Later a different person came up and asked me politely to sit because he could not see, told me he was not with the rude group, and I moved for him.
Another time I was invited to someone's house for dinner and we were told we could get what we needed from the fridge. Someone asked me to grab something specific for them, and I had it open nearly a half-minute looking for the requested item when the host screamed at me harshly "Shut the ******* fridge!!!" I ignored her until she screamed again 5 or 10 seconds later "I said shut the damn fridge!" So I simply said "I heard you the first time but I don't obey orders shouted at me by ********. Next time try politely." She didn't respond, but I'm pretty sure she was super pissed.
The above two paragraphs are examples of potential reactions by your friends if you mishandle the situation. And if they are like I used to be it might even explain why they are behaving the way they are; of course I cannot know for sure, and it depends on how you asked before and what they are like.
If you're not clear when you're being nice, or you're clear but being unkind unreasonably, then you should not expect the situation to resolve.
Initial Reaction
Stop touching my hair.
Bad. For good friends, the best this is guaranteed to do is help you right now, but that's not your goal.
Stop it! And don't ever touch my hair again! (very loudly and sternly)
Bad. Even for good friends, this might work or they might only take away from it that you don't respect them enough to ask nicely the first time.
I don't like my hair touched. Every time you do it, it bothers me. Don't touch it anymore. (In a calm, polite way, with no telling them off or freaking out.)
Good. And if they say or do anything against that, like continuing to touch it or giving an excuse or insulting you, then you just politely stand your ground.
Just don't touch it. I don't like to be touched, so don't ever touch my hair again.
If you part ways that day and come across them again later, remind them.
Remember not to touch my hair. I'll get grumpy if you do.
Secondary Reaction
So you've had your initial reaction. Now a day or two later they do it again anyway even though you asked nicely. This might be them not caring, or it might be them forgetting or having developed a habit of it by now and doing it without thinking.
At this point you might not know if they are sincere about accidentally violating you or if they are just being mean. If someone genuinely forgot or accidentally did it without thinking out of habit and you get cranky at them over it, you could undo any progress you've made.
For now, try to rebuke them as gently and politely as you can. Give them the benefit of the doubt.
(dodges friend's hand or pushes it away) Remember I said I don't want you doing that anymore. Don't do it again. Respect my boundaries.
I would suggest you respond to their negligence this way for at least 2 violations. I usually do for more than 2, but that's just me. After 2 of these, they've had at least 3 strikes.
If you decide "This is the last time. I'm not giving any more warnings after the next one." then you should let them know that was the last time and future violations will be met with more resistance.
Stop. You're not respecting me. Next time I won't be nice about it anymore."
Now you can tell them off or freak out
After all of the above, if they still do it then you've done your due diligence. If you've truly been polite and respectful while doing all the above, then you've given them ample opportunity to treat you like a friend and they failed.
If you don't want to just cut off the friendship at this point, you can respond to further violations with stern rebukes. Now you can yell at them, smack their hands away, tell them how lousy they are at respecting other peoples bodies. Now you have no shame in shouting "Get your damn hands off my hair!"
At this point, if you've yelled at them even once, you should think about whether they are really friends. Feel free to tell them that too.
You are disrespecting my body. You are being a bully. It makes me wonder if you are really a friend.
I once got someone to change their behavior by pointing out to her that she was being a bully. She didn't believe it at first, and I had to point out to her how she is repeatedly disrespecting someone and attempting to dominate social interactions, and that makes her a bully. When she finally understood she seemed ashamed. That's a good thing.
My experience with this
I have experience with this on both sides.
When I was about 5-10 I was a little jerk to certain people and ignored their demands about their personal boundaries. I was awful when I was very young. I regret it, and fortunately I grew out of that before my teen years.
When I was maybe 10-25 I was better, but I would still ignore any demands that I felt were made unreasonably (see two examples above). I'm trying to help you see it from that point of view. I'm not saying what I did when ignoring unreasonable demands was ok - I'm just trying to help get better results against it. Whether it's ok is a matter for a separate discussion.
That's my experience on your friends' side, but on your side I also have dealt with it many times and still do.
I have found that starting off gentle and polite and escalating does a few things. It...
avoids negative results if your friends are like I used to be
allows you to get a better idea of how respectful the other person is by how much you have to escalate before they respond positively
makes you the "good guy" since you were nice about it
avoids embarrassment or shame that comes from being overly dramatic and rude unnecessarily
Conclusion
Start off very gentle and kind, but also be very obvious about what your goal is (to never have your hair touched ever again). Remind them before they even do it. Rebuke them very gently and kindly the next couple violations. After that you can flip out on them and they have nobody to blame but themselves.