8

I'm trying to get some advice on how to go about implementing the same sort of "drag to reorder" behavior found on a UITableView but on a UIStackView.

So far this is what I'm thinking:

  1. Add a long press gesture recognizer to each of the arrangedSubviews
  2. On long press, add an overlay UIView as a subview to the main view and that fills the entire main view. This overlay view will act as a "canvas" of sorts as I'm dragging the temporary "reorder" view—we'll call this the tempView— around (more on that in 4)
  3. Set the alpha of the arrangedSubview to 0 (setting hidden = true would result in the UIStackView animating closing in as if it were removed.
  4. Add a tempView to the overlay view (see 3) with the same initial frame as the arrangedSubview
  5. Drag the tempView up or down and insert/remove the actual arrangedSubview at a calculated index based on the position of the tempView.
  6. Listen for when the long press state is .Ended and set the arrangedSubview.alpha = 1 and remove the overlay view from the main view.

Any suggestions? Any glaring holes that anyone sees? What would you change? What direction would you go to accomplish this?

Clay Ellis
  • 4,960
  • 2
  • 37
  • 45
  • 1
    would probably be a lot better if you just used a tableview, it handles the reordering animations and stuff really nicely, which will take a lot of work to replicate if you tried to do it yourself. is there a specific reason why you need to use a stackview? – Fonix Oct 16 '15 at 07:51
  • 1
    @Fonix I know it would be easier using a UITableView, but I'm trying to get this working on a UIStackView for the sakes (and pure joy) of getting it to work on a UIStackView. – Clay Ellis Oct 16 '15 at 09:15
  • Sounds good - Did you manage to get it built? I've started porting much of my code over to UIStackViews and it's been a huge win over my old code. Reordering of views in a UIStackView would solve one of my big issues currently. – Mof Dec 01 '15 at 05:32
  • @Mof I did actually! I'll throw up a github repo and send it your way sometime either tonight or tomorrow. – Clay Ellis Dec 02 '15 at 03:13
  • @ClayEllis Brilliant - I look forward to it. Thanks. – Mof Dec 02 '15 at 04:58
  • @ClayEllis a link would be cool. I am looking at doing the same thing and also considering drag reorder for a grid of horizontal UIStackViews nested inside a vertical UIStackView. – Swany Dec 04 '15 at 17:43
  • @Mof Sorry, I forgot to throw the repo up! Have at it! https://github.com/clayellis/APReorderableStackView – Clay Ellis Dec 06 '15 at 21:19
  • @Swany Sorry, I forgot to throw the repo up! Have at it! github.com/clayellis/APReorderableStackView – Clay Ellis Dec 06 '15 at 21:19
  • Let me know if you have any questions. There's still a little work to be done on it to make it fully capable. (For instance, only dragging up and down is supported right now. Horizontal dragging could be added in the future. My company, Appsidian, is creating a development kit we call APKit that will be released publicly soon. APReorderableStackView is part of APKit and once it released publicly the licenses will be updated. I give you permission to use this code despite the copyright (as well as anyone else who stumbles on this post). – Clay Ellis Dec 06 '15 at 21:22
  • Thanks Clay - I will have a play as soon as I can. Also look forward to checking out APKit when it's done. Good stuff. – Mof Dec 06 '15 at 22:14

1 Answers1

9

I've created a custom control to solve this problem that can be found here.

Please see my last comment on the question post regarding known issues and future updates.

Clay Ellis
  • 4,960
  • 2
  • 37
  • 45