There is no way for a creature to gain flying and *cancel* an attack. However, you can have a creature gain flying after your opponent activates their creature but before they declare their attack. Now, in actual game play, most players will declare their attack as they activate their creature or possibly even before they flip that action marker. When that happens, I would tell them that I am revealing my enchantment during the time in between those two steps (which they tried to skip over) and that they must pick a different action for their creature to perform.
It still doesn't cancel the attack, as they get to do something else, but their creature was stopped from doing what it wanted.
A flying creature does lose the flying trait when it attacks a non-flying creature, but only during the time it takes to complete the attack. This is primarily to allow counterstrike to work. After the attack is finished, the creature immediately regains flying.