How to scroll with UIScrollView with stopping only at pecific regular positions? - iphone

I have image that needs to be scrolled in uiscrollview but scrolling needs to be stopped only at specific points. Imagine I have long image of uitableview so I need to scroll it. What I'm thinking about: is it possible to stop scroll only at points where full row is visible at the top? If yes, then how to do it?

You need to set the coordinates to where you want the scroll to move. This is done here -
[scrollView scrollRectToVisible:CGRectMake(0, 50, 1536, 939) animated:NO];
So you need to specify the exact CGRect specs you need. But this will work for you.
If you turn animated:YES then the scroll to this new position is smoothly animated.

Related

UIScrollView Pagination on rotation

I have a UIScrollview with some subviews, on rotation I resize and all looks correct, the problem is that when I rotate even though I have correctly resized everything in viewwillrotate, my scrollview is between two pages.
A single touch on the scrollview aligns everything correctly, what am I doing wrong?
This can be fixed with setting content offset manually:
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

How do you programmatically force a scroll using UIScrollView?

I have a horizontal UIScrollview set up (meaning its not one that scrolls up and down but one that only scrolls left-right) and upon the app launching, I want this scrollview to scroll itself left, then right - kind of "demonstrating" its ability to scroll - and finally then stop, letting the user then take over and control the scroll manually with their finger.
Everything works - except for this on-load left-right demo-scrolling.
I'm not using Interface Builder for any of this, I'm doing everything with code:
//(this is in viewDidLoad:)
// Create the scrollView:
UIScrollView *photosScroll = [[UIScrollView alloc] initWithFrame: CGRectMake(0, 0, 320, 200)];
[photosScroll setContentSize: CGSizeMake(1240, 200)];
// Add it as a subview to the mainView
[mainView addSubview:photosScroll];
// Set the photoScroll's delegate:
photosScroll.delegate = self;
// Create a frame to which to scroll:
CGRect frame = CGRectMake(10, 10, 80, 150);
// Scroll to that frame:
[photosScroll scrollRectToVisible: frame animated:YES];
So the scrollView loads successfully and I'm able to scroll it left and right with my finger - but it doesn't do this "auto-scroll" like I was hoping it would.
I tried calling the scrollRectToVisible before and after adding the
scrollView as a subview - didn't work.
Thinking maybe this should
happen in loadView and not in viewDidLoad? But if so, how?
Any ideas?
A couple of ways to do this:
Use scrollRectToVisible:animated: for a zero X value and then a largeish positive one, then set it back to your middle point. You can call the method after the scroll view is added as a subview (probably before too, but best be safe). You'll need to calculate what value of X is off-screen to the left using your contentSize and knowledge about how wide your various elements inside the scroll view are.
Since you're just doing a quick animation and will give the user control, this can really be a quick-and-dirty animation, so you could use setContentOffset:animated: to force the content of the scrollview to move relative to your current view position. Move the elements right for a left scroll (positive X value), then left twice as far for a right scroll (negative X value), then set the offset back to zero.
You shouldn't put either of these in viewDidLoad or loadView; put them in viewDidAppear, which is after your scrollview is actually drawn on screen. You only care about animations when the user can see them!

move an image with touching like a scrollview

Well I have an image and I I want that when I touch it and then I move my finger it follows it.but I want that it follows it only vertically like a vertical scrollview(but I don't wanna use a scrollview).How can I do this please. sorry for my english I'm french :/
Why wouldn't you subclass UIScrollView for your view that displays the image? If you do it's pretty simple: define its size so that the horizontal length doesn't exceed the screen length. You'd have to also remember to change this if you allow rotation of the device/view to landscape. Suppose the width of your UIScrollView is 320, you would do this:
CGSize scrollableSize = CGSizeMake(320, myScrollableHeight);
[myScrollView setContentSize:scrollableSize];
Where myScrollableHeight is the full height of the image.
I've never used it but if that doesn't work there is apparently a method you can provide for your UIScrollView class that is called whenever the user attempts to scroll and you can just not adjust the X-coordinate there:
float oldX; // make this ivar in your .h file
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
[aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.y, oldX)];
}
Take a look at Apple's MoveMe sample. This should give you an idea. If you only want it to move vertically, only change the y value while dragging.
Just adding a panning gesture to the image parent view and moving the image.center (only the y in your case) should be enough. Look at apple documentation for more details

UIScrollView auto-scrolls to top after SetContentSize during orientation change - how can I prevent that?

I have a UIScrollView in which vertical only scrolling is enabled. I'm displaying a grid of buttons (similar to the photo viewer image grid). The grid has to be drawn differently based on screen orientation, so that all of the screen real estate is used. Given the size of my buttons, I can fit 3 per row in portrait, and 4 per row in landscape.
I reposition all of the buttons in: willRotateToInterfaceOrientation:duration and then call: setContentSize: on my UIScrollView. Everything seems to work just fine, with the exception of the auto-scrolling that occurs after the call to SetContentSize:. In other words, let's say I was in portrait, and scrolled 3/4 of the way down the list, when I rotate to landscape, it auto-scrolls all the back up to the top.
The interesting thing is, in the same scenario, if I were to do a small flick scroll up or down, and then immediately rotate the device, the scroll view redraws correctly, and retains the current scroll position!
So, to be clear, the culprit here seems to be SetContentSize:, but obviously I have to call that for the scroll view to scroll correctly.
Any ideas on how I can retain the current scroll position?
Thanks!
You might try implementing the UIScrollViewDelegate method scrollViewShouldScrollToTop:, which tells the caller whether to scroll to the top or not. You may have to have some flag in your delegate that indicates if a rotation is underway or not, because under normal conditions you may actually want the ability to tap the status bar and have the scroll view scroll to the top.
If, on the other hand, you don't ever want the scroll view to scroll to the top automaticlly, simply implement that delegate method and have it return NO.
I had this problem, just did this and it works well for me
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//calculate percentage down scrollview currently at
float c = self.scrollView.contentOffset.y / self.scrollView.contentSize.height;
//reposition subviews in the scrollview
[self positionThumbnails:toInterfaceOrientation];
//set the new scrollview offset to the same percentage,
// using the new scrollview height to calculate
self.scrollView.contentOffset =
CGPointMake(0, c * self.scrollView.contentSize.height);
}
In my case, I have a scrollview with a fixed width to device size, and variable height

scrollRectToVisible doesn't work with keyboard and (next/previous)toolbar. Please see the picture

scrollRectToVisible doesn't work with keyboard and (next/previous/done)toolbar. Please see the picture.
[scrollview scrollRectToVisible: textFieldRect animated:YES];
The method scrollRectToVisible: is doing the correct thing. It's scrolling the view to the point where the CGRect specified is within the visible section of the view. But, here's the thing - you're positioning another view over the top of the scroll view, so a part of the scroll view's visible area is obscured. The scroll view doesn't know about this, it only knows about it's visible section independant of any other views.
A solution to this may be to offset your textFieldRect CGRect by a given amount, to get the scroll view to scroll a little further in a given direction. You could, for example, use the size of the onscreen keyboard to calculate this offset, or perhaps the size of the translucent view that can be seen in your screenshot?