I just bait the Gym Boss in to attacking by running into his attack zone and instantly turning around to run away, if your too close to a dead end then baiting him into attacking then running past him when the attack lands will usually get me through, although sometimes you would still be hit during the attack at some point, like that one time i clearly was out of his attack range but he still managed to clip me.