I have the same issue, it randomly can't seem to focus.
After looking it up it seems like a rather common issue, the mechanism is stuck. Hitting it against your hand to unstuck it usually fixes the problem.
Have it replaced if it happens too often and your phone is still under warranty.