This is a very common behaviour in cats & lots of people say that their cat is fine being stroked for a few seconds & then turns & swipes at them or bites them with no warning.
Actually they do warn you first but you have to be very observant of their body language to anticipate when it is about to happen & stop touching them before they react.
Whilst most cats love being stroked & petted, it is really a form of exchanging scent. Cats have scent glands on their face, cheeks, between ears & eyes & on their flanks. By rubbing up against us or when we stroke them, we are putting our scent on them & collecting their scent on us ~ sort of making a communal scent "pot pourri". It gives the cats a sense of security & marks us all as part of their social group.
BUT this scent marking usually only goes on for a few seconds & some cats think it is very odd when we persist in prolonging this scent exchange behaviour. Their reaction is "stop, enough! We 've done that, now you're just invading my personal space!" Hence the swipe or bite & hence the purring as it is quite nice & normal at first.
The best thing to do is only stroke your cat for a few seconds & stop well before you get any sort of reaction. Don't use your hands or feet to play with your cat ~ use fishing rod type toys instead. If you want to prolong the petting you will need to do it gradually, build up by a few seconds every few days.
It doesn't mean your cat doesn't like you, it's just a way to tell you to stop.