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:
- Add a long press gesture recognizer to each of the
arrangedSubview
s - 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 thetempView
— around (more on that in 4) - Set the
alpha
of thearrangedSubview
to 0 (settinghidden = true
would result in theUIStackView
animating closing in as if it were removed. - Add a
tempView
to the overlay view (see 3) with the same initial frame as thearrangedSubview
- Drag the
tempView
up or down and insert/remove the actualarrangedSubview
at a calculated index based on the position of thetempView
. - Listen for when the long press
state
is.Ended
and set thearrangedSubview.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?
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.
来源:https://stackoverflow.com/questions/33165079/uistackview-drag-to-reorder-arranged-subviews