iOS UIScrollView with multiple pages visible - ios

I'm having a little issue with scrollview pagination.
My intention is to have 3 pages visible on screen and when I scroll right only on page is scrolled, following the example below, scrolling right will display page 2, 3 and 4 on screen:
However I don't know how to display mutiple pages at the same time, at the moment I have it like this:
Obviously like this is not what I want.
To achieve the desired functionality I tried making the scrollview's frame the size of the page I want (1/3 of the screen width) and setting the clipToBound to NO so the other pages are visible. This indeed shows 3 pages in the screen; however since the scrollview frame is only 1/3 of the screen I can only swipe in that area, I would like the swipe area to be the whole screen width.

You're on the right path. Now you can try manipulating UIPanGestureRecognizer of your scrollView, say, re-attaching it to scrollView's superview.
Alternatively, take a look at iCarousel, it can be perfectly customized to suit your needs.

My solution in the end was the following:
I took my initial approach.
I disabled the scrollview's scrollEnabled property.
Added swipe gesture recognisers to the scrollview.
When the gesture is made I modify the scrollview's contentOffset to move 320/3 pixels to the right or left.

Related

How to create UIScrollView with different page sizes (page snapping, bounce)

I'm trying to create custom vertical UIScrollView which could handle multiple pages with different page heights.
Assumptions:
page height is equal or greater than screen height
if page is taller than screen height, it scrolls as usual UIScrollView – with bouncing on top and bottom
if user ends up scrolling and "page break" is in the middle of screen
if there is no velocity - page snaps to closest
if there is velocity - page changes to one in direction of swipe
I've tried many approaches to achieve this, but I've stumbled upon many UIScrollView quirks, which make it hard.
Problems:
UIPanGestureRecognizer has unreliable method for getting velocity (velocityInView:)
scrollViewWillEndDragging:withVelocity:targetContentOffset: method gives me headache, because it arbitrarily can destroy my attempts to animate setting content offset
I don't know how to achieve bounce in one of the middle pages, I'm afraid i would have to rewrite whole scrolling handling
when I try to override setting content offset when UIScrollView is decelerating, what I get is
my content offset is set
deceleration continues beyond content offset I set
Bonus
I have also tried putting UIScrollView inside UIScrollView as a page, but this approach was also pain in the neck. For example when I was at the bottom of inside scroll, then i scrolled down a bit, put my finger away and quickly grabbed again and scrolled upwards, the outer scroll received touch, which messed up inside scroll presentation.
Does somebody have any idea how to do this? Any tips will be helpful as I'm completely stuck...
Try this. Might help. Based on the Circa news app.
https://www.cocoacontrols.com/controls/rscircapagecontrol

UITextView won't scroll from outside particular area

I have a UITextView with approx 100 lines(for testing purpose its static) which scrolls fine but when i try to scroll from approx 150 its from the bottom i can not scroll. That means it lets me scroll from only certain area of the textview. for example see the image below. If i try to select and scroll above 3 line from top, i can scroll but below 3rd line, It won't let me scroll. I went through documentation but couldn't find anything that makes sense.
Try resizing the frame of your UITextView so it does not overlap the UITabBarView. If you create this in interface builder, you can resize it there.
if there is some area that is not taking ui touches, thAT means, your UITextView is being overlapped by another view. try:
[self.view bringSubviewToFront:textView];
also think about Enrico's answer.
Put Background color for TextView. It will give you extact idea of TextView Complete framing and is it sub view of background or foreground.

What is the best way to animate the size of sub view in a UIScrollView?

I've got a scrollview that allows the user to scroll between different pages and then tap on one to have it expand so that they can read the page in full, a little like how one changes tabs in Safari on the iPhone. Changing the frame size of each sub view is a bit of a pain when rotating as the scroll position is getting lost as the content size of the sub view has to change too. I was wondering if there was a more effective way of
resizing the views for entering 'viewing' mode or rotating the device.
The solution to your first problem is when you want to expand the view, pull it out of the scrollView then add it to self.view.subviews with an appropriate frame, then animate the frame to fill the screen. When done with it do the reverse, shrink it, then when its back to the appropriate size stick it back in the scrollView.
If for some reason this does not work - the scrollview is showing other stuff that gets moved when you remove the view, then instead of just removing your view from it, create a simple UIView of the same size as the view you will expand, and essentially replace the view you pull out with the "placeholder" view.

Triple Nested UIScrollView paging issue

Backstory
I have an iPad app that needs to allow the user to navigate through groups of images. Each group is laid out in its own vertical UIScrollView (paged) so the user can swipe up and down down to see each image. Each of the group UIScrollViews is placed in a single (only one exists in the app) outer horizontal UIScrollView (also paged). This works great.... I can swipe up and down to view the images in a group and swipe left and right to go to the next or previous group.
Problem
The problem started when I needed to add zooming for each image. I accomplished this by placing each image inside its own UIScrollView. When the image is zoomed I can pan around the image and when I get to the top or the bottom of the zoomed image the group's vertical UIScrollView pages to the next or previous image as expected. Unfortunately the outer horizontal scrollview will not page to the next group when the image is zoomed and I pan to the leftmost or rightmost edge.
Is there a better(more correct) approach than triple nesting UIScrollViews or can I somehow forward touches to the outer horizontal scrollview?
Any help or suggestions would be greatly appreciated.
hope i'm not too late but I think I have a solution for your problem.
Here you can find an Xcode project demonstrating the scrollview setup you have, your problem and the proposed solution: https://bitbucket.org/reydan/threescrollviews
Basically the solution was to add 1 pixel to the contentSize.width of the vertical scrollviews. This forces the vertical scrollview to scroll a little when you pan to the edge of the zoomed image. It scrolls a little and then continues to the next vertical scrollview.
If you download the project you will see that I've created some scrollviews in the viewDidLoad method. There, I create one horizontal scrollview containing 3 vertical scrollviews, each containing 5 images. Each image is actually incapsulated in a scrollview to enable per-image zooming. In total... triple nested scrollviews.
I've also left some colored borders so that I can easily see how each scrollview scrolls.
the magenta = horizontal scrollview
the white = vertical scrollview
the blue = the image scrollview (the one that contains the image and allows for zooming)
the red = the UIImageView
You will see that I've tagged each image scrollview with value 10. This is used in the implementation of - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView delegate method where I return nil unless the event came from one of the image scrollviews.
If you have any questions about the project I made feel free to ask.
In the end, I would like to say that this browsing method is a little quirky for me as I sometimes scroll in the unwanted direction. Often I think I flick my finger vertically only to find the scrollview going left or right because it interpreted some tiny horizontal movement I had.
The problem I found with paging enabled for both horizontal and vertical movement is that the scrollviews are direction-locked, or so it seemed to me.
EDIT:
Today I've investigated the problem even more. These are my conclusions:
it's not a problem with zooming, it's a problem with having larger content in the innermost scrollview than the visible area(you can try this by zooming or simply initializing the content size larger than the bounds). This enables panning inside the inner-most scrollview and completely changes the behaviour of the touch events.
the bounce for a scrollview flag affects the behaviour of the panning(dragging) gesture when it reaches the edges of the content. If bounces=false then your panning gesture will stop at the edge, not forwarding the drag event up the chain (and thus not scrolling the parent scrollviews to show you other images). If bounces=true then, when you reach the edge and continue to drag the events will be forwarded to the parent scrollview and that scrollview will also be dragged. However, I've found that the dragging while bouncing reduces the distance dragged by aproximately 50%. This also happens in the Photos app.
if you start the dragging while the innermost scrollview is at the edge of the content then the scrollview is smart and will forward all events to the parent scrollview.
for some reason, triple nested scrollviews are problematic as the events are simply not forwarded between the topmost and middle scrollviews while panning inside the innermost scrollview. I have no idea why.
My solution with that +1 pixel to the content size, partially solves the problem.
EDIT 2013
Boy, these scrollviews are something out of this world :(
After more than a year of searching (just kidding... it was actually 2 days) I think I found a good elegant solution to the triple nested scrollviews.
I created a test project here:
https://github.com/reydanro/TripleNestedScrollViews
Inside the app, there is a switch which you can use to test with/without the fix.
The setup I am using in my app is a little different than this question. I have 1 vertical paged scrollview. Inside it, I have multiple horizontal paged scrollviews.
Inside some of the horizontal scrollviews I have another vertical paged scrollview.
Without the fix, once you get to the page with the inner-most scrollview you are pretty much stuck there as the vertical scrolling gestures are not forwarded to the outer-most scroll.
The fix is a custom UIGestureRecognizer that you need to add to the inner-most scrollviews. This recognizer follows touch events and if it detects a drag beyond the contentArea, then it will temporarily disable the rest of the scrollview's recognizers. This is the only method I discovered to make the scrollview forward the events up the chain
The gesture recognizer code is very rough with limited customization but should get the job done. At the moment I am focused on the app I develop, but will continue to update the repository.
PS: I haven't tested what happens with zoom but I see no reason why this method should not work (or be adapted to work).

My UIScrollView bounces back to the top when i let it go

I have two problems with my UIScrollView, first, when i launch the app, the first screen i see is the one which contain the UIScrollView, so i try to scroll but i couldn't, it like if it is locked, so i navigate to other screens in the app, and when i go back to the first screen it scrolls very well.
the second problem is that when i scroll down and since i let the UIScrollView go, it bounces back to the top and so i can't interact with the content I just scrolled to.
check the #property contentSize. I bet it is set to CGSizeZero.
The height must be set to the maximum Y of the last element (in terms of Y-position) of your scrollView subviews.
You might want to have a look at this page, it has a lot of information regarding the UIS

Resources