How to drag a view inside a UIScrollView with scrolling near the edges?

I am trying to create some kind of timeline representation, for example, in video editors: the multimedia elements in a line that are UIView . I can successfully drag and drop these views inside the currently visible scroll part using UIScrollView touch events like touchesBegan and touchesMoved . I want to scroll the scroll as soon as the subview is dragged to one of the edges of the scroll. The best thing I can think of right now is to create a timer that will scroll the view while the user holds the subtitle with his finger near the edge of the scroll.

There are many questions on one topic, but I could not find the one that covers the scroll.

Is there a good way to do this? Should I use gesture recognizers?

Thanks in advance.

+6
source share
2 answers

Actually what you want is an event time. Once the user is on the edge of the scroll, you start a timer that regularly increases the contentOffset . If you don’t like the animation results (I think you are using setContentOffset:animated: :?), Just try a different time and distance from the animation. I think you need to try a few different settings. What I will try first is 1px at a time. Maybe every 0.3 seconds?

If this does not work, you can also try another “extreme” one. Start one animation when the user reaches the edge, which animates the contentOffset to the end of the contentSize. But for large periods of time, the movement is slow. If the user stops dragging or moving from the edge, stop the animation at its current position. It would even be a solution without a timer, because the animation would be your timer.

+8
source

I seriously doubt that gesture recognizers will be part of a good solution to this, as they are usually most useful with careful gestures.

I do not think that I can improve my general direction, based on the assumption indicated above that you are looking for a continuous / gradual scroll.

Instead, I suggest you consider using this approach to use scrollable swap. When your user drags an object to the edge of the scroll, make scrollview move one page in that direction (by setting it so that the contentOffset moves in that direction according to the scrollview's borders). When this happens, move the object slightly out of the “hot zone” on the edge of the scroll, so that the user has to explicitly express that they want to move another page or something in that direction, that is, because the design approach depends on these “swap events” ", you need to implement some kind of rigid system in order for the user to continue paging.

I suppose you could use a timer in the same situation, so if the user maintains the position and touches another second, you will be back on the page.

0
source

Source: https://habr.com/ru/post/911042/


All Articles