Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to create some sort of timeline view like in video editors: media elements in a row, which are UIView's. I can successfully drag these views inside currently visible part of scroll view using UIScrollView touch events like touchesBegan and touchesMoved. I want to scroll the scroll view once subview is dragged to one of the scroll view edges. The best I can think of now is to create a timer that will scroll the view while user holds the subview with the finger near scroll view edge.

There's a lot of questions here on the same topic, but I was unable to find one that covers scrolling.

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

Thank you in advance.

like image 839
anticyclope Avatar asked Mar 18 '12 19:03

anticyclope


People also ask

How to scroll content in UIScrollView with auto layout?

However, UIScrollView scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the edges within a scroll view actually refer to the edges of its Content view.

What are the visible edges of a scroll view?

Note: In general, Auto Layout considers the top, left, bottom and right edges of a view to be the visible edges. However, UIScrollView scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the edges within a scroll view actually refer to the edges of its Content view.

What are the UIScrollView constraints?

These constraints tell the UIScrollView the boundaries of your content (sets the contentSize of the UIScrollView ). You usually only want your content to scroll in one direction. In my case, I want the scroll view to scroll vertically. Therefore, I need to set the width of my single content view to be the width of the scroll view.

What is the Scroll View in uitextfield?

The scroll view allows us to scroll the content into view. We first need to keep track of which text field is currently being edited. You can do this in many different ways, but I chose to add the view controller as a delegate to the UITextField s.


1 Answers

Actually what you want IS a timed event. As soon, as the user is at the edge of the scrollview, you start a timer, which regularly increases the contentOffset. If you don't like your animation results (i guess you're using setContentOffset:animated:?), just try another timing and distance of animation.. I guess you have to try some different settings. What I would try first is 1px at a time. Perhaps every 0.3 second?

If that doesn't work you could also try another "extreme". Start a single animation, when the user reaches the edge, which animates the contentOffset until the end of the contentSize. But over a large timespan so the movement is slow. If the user stops dragging, or moves out of the edge, stop the animation at the current position. That would even be a solution without a timer, because the animation would be your timer itself.

like image 90
calimarkus Avatar answered Sep 25 '22 02:09

calimarkus