Its been a pain in the ass to get a working solution for this problem. On a view i have around five scroll views with maximum zoom upto 5 . What i want is if a user has zoomed into a particular region of image , and then comes back , scroll view sets the image wherever it was left . I have tried using content Offset and content Inset property but that does'nt help.
All you need to do is to save somewhere both the value of the scroll view's contentOffset property and zoomScale.
When you want to move your scrollview back to that position you just do
[myScrollView setZoomScale:lastZoomScale];
[myScrollView setContentOffset:lastContentOffset];
Related
Is there a way to stop an object in a UIScrollView from moving as a result of the UIScrollView responding from user interaction? Essentially I want some things in the scrollview to move but others to remain frozen in place. They all need to be within the scrollview, so moving them to a different view is not an option.
For anyone else having the same problem, I found an answer. I put the objects which I wanted to remain still in a UIView. I then used the UIScrollView delegates to get the initial content offset when the scroll was about to begin and subtracted the current content offset as the scroll view was scrolling to get the change in the content offset. Afterwards, I sent that content offset to the container view I put all my objects in and subtracted the change in content offset from the container view's position. The result is everything in the container view remaining perfectly still.
add to the desired fixed object a constraint in relation to the main view. This would pin it to that view.
have looked but can't find a solution to this on SO.
The problem: I would like to add buttons to a UIImageView that is zoomable however when the image zooms in, the buttons remain in the same place on the image but do not zoom (ie they stay the same size). This is similar to what Maps does if it has many pins on the map, when you zoom in on the Map if the pins are close together they spread apart and stay the same size, rather than zooming to huge sizes with the map!
My attempt: I understand how to make the image zoom, through use of a scroll view however I am unsure of what the view hierarchy should be. The button locations depend on the image and so you cannot add the buttons to just the scroll as they wouldn't move when zooming. However, if I add the buttons to the image view then they zoom with the image (since I return the image view for the delegate method viewForZoomingInScrollView). I have tried tinkering with an overlayer for the buttons with clues from Zooming a background image without zooming the overlay but haven't found anything that works yet.
Any help is greatly appreciated, thanks a lot!
You should not add the button as a subview to the scroll view. Instead, try adding the button as a subview to your scroll view's superview.
[superview addSubview:scrollView];
[superview addSubview:button];
Your view hierarchy will look like this:
<superview>
<scrollView>
<button>
Since the button is not a subview of the scroll view, it won't get zoomed.
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.
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.
Ok. here is the story:
I have a UIWebView in full screen, width = 768
It loads several images and those images are in one line, which means the UIWebView will scroll only horizontally.
Now, I set the UIScrollView inside the UIWebView to pagingEnabled = YES. So the scroll on the UIWebView will move page by page.
The problem is that every image's width is about 900. I won't scale them down and if I scroll the UIWebView, from the 2nd page on, always 132points of previous image will show. This is not good for me.
So how can I manipulate the UIWebView or the UIScrollView inside the UIWebView so that each scroll will move a page of 900 points, if the view's frame width is 768?
The constraints are:
I can't change its contentSize
I can't change its bounds or frame
I say these constraints because UIWebView will change them on its own purpose when it loads contents.
Anyone would like to join this brain storming?
Thanks
I'm thinking you're going to need to set a UIScrollView delegate that tracks touches beginning and ending. More specifically, scrollViewDidEndDragging:willDecelerate is what I'm thinking you'll need to use, as it fires the instant a user lifts their hand from the device.
First, pagingEnabled=NO seems obvious, we're going to have to control our own paging. Which, I don't feel is too tough. Track the direction of the scrolling in scrollViewDidScroll:, so that we have some global set:
BOOL isGoingRight = YES or NO (NO means the last movement was to the left)
float PAGESIZE = 900.0;
Here's how my scrollview delegate method would look like that handles paging.
- (void) scrollViewDidEndDragging:(UIScrollView*)_scrollView willDecelerate:(BOOL)willDecelerate {
float offsetProportion = _scrollView.contentOffset.x/_scrollView.frame.size.width;
//depending upon our direction, round up or down
int page = isGoingRight? ceilf(offsetProportion): floorf(offsetProportion);
//manually finish the scroll
[_scrollView scrollRectToVisible:CGRectMake(page*PAGESIZE, 0, _scrollView.frame.size.width, _scrollView.frame.size.height) animated:YES];
}
I didn't test this, you may or may not need to disable touches at points.