iOS: Transfer ownership of UIView while touches happening?
ios, touchesbegan, touchesmoved, uiscrollview, uiview
Solution
Instead of:
[transferView removeFromSuperView];
[newParentView addSubview:transferView];
Use only:
[newParentView addSubview:transferView];
The documentation states: "Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview."
Therefore there is no need to use removeFromSuperView because it is handled by addSubview. I have noticed that removeFromSuperView ends any current touches without calling touchesEnded. If you use only addSubview, touches are not interrupted.
Problem
The moment I receive `touchesBegan`, I want to `removeFromSuperview` the view that was touched and `addSuperview` to a new parent view, and then continue to receive touches. However I am finding that sometimes this does not work. Specifically, `touchesMoved` and `touchesEnded` are never called. Is there a trick for making this work correctly? This is for implementing a drag and drop behavior, where the view is initially inside a scroll view. Thanks.