There is no indication in Scripture that God's spirit would leave you when you sin, and come back when God forgives. In fact, there are several scriptures which indicate the opposite.
We are told in Hebrews 13:5, that God "will never leave you, nor forsake you."
We have the example of the Apostle Peter, who sinned by shunning the Gentile Christians when Jewish Christians visited. The Apostle Paul "withstood him to the face" and yet Peter called him "our beloved brother Paul" (2 Peter 3:15). The Apostle Peter showed Christian love in this situation, and accepted the rebuke as a loving gesture from Paul. This is further shown by Paul's advice to the brethren in Galatia (Galatians 6:1,2), "Brethren, even if a man is caught in any trespass, you who are spiritual, restore such a one in a spirit of gentleness; each one looking to yourself, lest you too be tempted. Bear one another's burdens, and thus fulfill the law of Christ."
God does not wait to forgive. He showed this in the parable of the Prodigal Son. After he had sinned and misused his inheritance, and realized what a mess he had made of things, he learned he was not worthy to be called a son, but wanted to be his father's servant. But, when the father saw him a long way off, he felt compassion for him and embraced and kissed him. (Luke 15:11-32) The father had already forgiven his son, and was watching for him, so that when he saw his son far away, he ran to meet him and kissed him to show his forgiveness.
When God proclaimed his character to Moses in Exodus 34:6, he said, “The LORD, the LORD God, compassionate and gracious, slow to anger, and abounding in lovingkindness and truth.” God is forgiving and merciful, and loves His people. He does not leave them, or take His spirit from them. He will discipline them as a loving father does, so that they will learn and grow. But, only they can leave Him….He will not leave them.
The only reason for God's spirit leaving someone is shown in Hebrews 6:4-6, "For in the case of those who have once been enlightened and have tasted of the heavenly gift and have been made partakers of the Holy Spirit, and have tasted the good word of God and the powers of the age to come, and then have fallen away, it is impossible to renew them again to repentance, since they again crucify to themselves the Son of God, and put Him to open shame." It is only those who have been enlightened, tasted of the heavenly gift, and been made partakers of the Holy Spirit who can lose God's spirit. And they can only lose it by falling away…denying Jesus' ransom price for all. It is impossible for salvation to be applied to them a second time, therefore they cannot receive God's spirit again.