Drawing rubber band line during user drag
In my iPhone app, I would like the user to be able to "connect" two of my views by:
1) starting a drag in View A 2) as they drag towards View B, a straight line with one end in View A and the other end under at the current drag point, animates in a rubber-band fashion 3) when/if they release in View B, the line is then shown between the two views
I've seen examples of dragging and dropping views, and other examples 开发者_StackOverflow社区of animations, but I haven't seen one that is a simple example of this kind of user-directed animation. Any pointers towards examples or the specific docs I should be looking at would be appreciated.
If this turns out to be trivial - my apologies. Although I've done quite a bit of development, I'm just getting started in the iPhone SDK and Core Graphics.
Turns out it is pretty easy - don't think of the line as belonging to either view, create a third view that is transparent and not opaque, place it over the top of the other two views. It could be full screen or you could calculate the size and position that just covers your views. Detect taps in this third view and use core animation to display a line from the point you started drawing to the point the line ends. When the line ends then you can detect whether the input was valid and place start/end points in the appropriate views. Functions that you will find especially useful in this process are UIView convertPoint:toView:
and beginAnimations:context:
.
It will probably keep things easier if you leave the line drawing as part of a dedicated view and add lines to it as they are accepted, rather than try to record the lines as part of the unrelated views that you are connecting with the lines - probably you want an array or similar containing CGFloats so that you can recreate the line view as necessary using drawRect:
.
精彩评论