The 26-year-old joined Swansea on a free transfer from Marseille in June 2015 and scored 12 goals during his debut Premier League season with the Welsh side.
“The club have a lot of ambition and have a great manager,” said Ayew. “They want to become a bigger club every year and with the new stadium and a new training ground, you can feel they are moving on to another level.”
West Ham have also moved to solve their defensive worries by completing the signing of left-back Arthur Masuaku from Olympiacos for £6m on a four-year deal.
A new left-back had become a priority for Hammers boss Slaven Bilic after Aaron Cresswell was sidelined for up to four months with a knee injury.
The prospect of playing alongside playmaker Dimitri Payet was a major contributing factor in former France Under-19 defender Masuaku, who won two Greek Superleague titles while with Olympiacos, opting for West Ham.
He said: “To be able to play with players like Dimitri Payet, I was thinking on the way here, it’s going to be crazy. I can’t wait to play with him and also all the rest of the team.”