Using a commutator
To reverse the electrical direction of a coil between two magnets, you can use a commutator.
The rotary action of the two halves of the commutator allows the current to switch from one way just before the wire coil is verticle to the other way just after the wire is vertical.
Below we have the wire coil which has switched electrical direction and the forces on the wire have reversed.
The coil reverses the direction of force every half-turn as the commutator reverses current every half-turn, thus maintaining continuous spin.