Loans have no effect on the gains on a purchase and sale.
So, you buy something for $100K. It's now worth $150K. You want to sell it and roll the $50K gain into a new property. Say the new property is $200K. I believe it must be worth more than the one you're selling to do a 1031. That's a legit transaction, and you avoid the tax on the $50K gain. Ignoring costs, you put $100K into the first property and another $50K in to buy the second.
Now, with loans. Say you borrowed 80% for the first one, so you have $20K in and a $80K loan. Now its worth $150K. Lets say you refi for $120K (80%, as if you could get that these days). You walk away with $40K after paying off the existing loan, plus any paydown on the original loan. Now you sell for $150K. You pay off the $120K loan, leaving $30K (still ignoring costs) in cash and a $50K gain. If you only roll the $30K in cash into the new property, you're short by $20K. So, you either need to come out of pocket to make that up, or its taxable "boot" from the transaction.
So, it works out exactly the same. You put $20K in. On the refi, you got that $20K back, plus $20K from the gain. When you sell, you have to contribute the gain you took out on the refi, or you pay taxes just as if you'd taken that on the all cash deal.