If he allows you to be with him, ask him what's wrong.
Do not complain about his action, you don't want to trigger the mood.
Give him real space when he need one, and be patient.
Understand that people with mood swing often have no idea what's bothering them. Do your best to make your boyfriend happy. When there's any underlaying problem, help him solve it as soon as possible.
When it starts getting too much, express your feelings about it. Tell him that you've reach the end of your ability to cope with the demoralizing and confusing issue.
Advice him to seek professional support, if it becomes too often. If it involves rage or violence, or suidal acts, look for a professional help yourself.
Always be the one to break the silence after the negative swing by wrapping your arms around him. Tell him you love him and are sad to see him distressed, and you can do anything to help, if he can just talk to you.
If you start noticing the symptoms of clinical mood disorder or bipolar personality, talk to him about meeting a doctor.
If things don't improve, leave the relationship. There's always an option. And does not suppose to be about just one person. Love is a 2-way path. Pull yourself out of the bondage.
Most of his mood swing may not have anything to do with you. Just relax and exercise calmness as an effort to let him calm too. He might ignore you but he's ignoring everything. So it's not just about you or your relationship.