You are classed as resident in Spain if you spend 183 days in the country not necessarily consecutively, but cumulatively. [Spanish tax year runs 1 January - 31 December]. Furthermore if one spouse is living in Spain the other spouse is automatically deemed resident also.
You are classed as resident in the UK if you spend 183 days in the UK in any one tax year [6 April - 5 April]. Once you have the 183 days in then you do not loose residential status by temporary departures overseas. So going back for four months at the end of a tax year is a mere 120 days away so you definitely won't infringe any UK residence status. I would suggest that you to ask your UK tax office for a confirmation that you are actually resident in the UK for tax purposes to strengthen your position with the Spanish tax authorities in the event of an inquiry.
Please note that there is a Double Taxation Treaty between the UK and Spain which precludes the same moneys being taxed in both jurisdictions; any tax paid being allowed as a tax credit against a liability on the same sums in the other.
I do hope I have helped throw some light on your situation.