How to create a UIScrollView of infinite scrolling? - iphone

I have a UIScrollView of size 320*460 and with content size 1024*1024.
I can place 25 images of 256*256 in it with the 13th picture shown at the centre of the
screen when it loades with bits of surrounding pictures around it.
When i swipe to any side I want it to appear just like the mapView. With new images
appearing and showing.
How can I do it?

It's very easy & somewhat tricky...
You don't need to define the specific size for the ScrollView.....
Generally we use to define ....as per the Examples of Apple
//#define SCROLLVIEW_CONTENT_HEIGHT 460
//#define SCROLLVIEW_CONTENT_WIDTH 320
which is of no use...if you need the infinite height..
just set the height of the ScrollView dynamically as per the Objects added to the ScrollView....
No need to set the predefined height for this...take the dynamic height....
scrollview.contentSize = CGSizeMake(320, txtView.contentSize.height+450);
CGPoint topOffset = CGPointMake(0,0);
[scrollview setContentOffset:topOffset animated:YES];
Hope this will surely work for you..
Good Luck :)

Checkout the sample code called 'Tiling'. It might be what you're looking for.

When scrolling stops, couldn't you just adjust the bounds so that you are now "re-centered" with your new offset?
Presumably you can't scroll more than so many pixels before your finger hits the edge of the screen, so you only need bounds that a few hundred pixels outside the original center.
Once you have a new center, adjust your bounds accordingly, retiling as necessary.

Related

How to resize UIView while moving, just like SnapGuide?

I've a scroll view which has several UIViews that shows information to the user. This scrollview supports only horizontal scrolling and when user starts scrolling from left to right or vise versa, the center view should be bigger and the previous and next views should be smaller (as shown in the below view).
I'm able to do this by changing the frames of all the views that appear on the scrollviews. But the animation is not smooth. I just want to resize the views while scrolling according to their position. Is there anyway to do this? Any help greatly appreciated.
Well you could look at these links
UIScrollview make the current Image Larger
Or I can provide a better solution to you with iCarousel. Please refer to it and I think this might help you , and its pretty easy to integrate too :)
This might be what you are looking for CGAffineTransformScale. You need to set your UIViews transformation property, for example
CGAffineTransform transformRatio = CGAffineTransformScale(CGAffineTransformIdentity, factorX, factorY);
myView.transform = transformRatio;
factorX and factorY are the values by which to scale your views width and height.
When you view starts scrolling in and out you need to set the corresponding factors.

iPhone Infinite Vertical Text Scrolling [duplicate]

I have a UIScrollView of size 320*460 and with content size 1024*1024.
I can place 25 images of 256*256 in it with the 13th picture shown at the centre of the
screen when it loades with bits of surrounding pictures around it.
When i swipe to any side I want it to appear just like the mapView. With new images
appearing and showing.
How can I do it?
It's very easy & somewhat tricky...
You don't need to define the specific size for the ScrollView.....
Generally we use to define ....as per the Examples of Apple
//#define SCROLLVIEW_CONTENT_HEIGHT 460
//#define SCROLLVIEW_CONTENT_WIDTH 320
which is of no use...if you need the infinite height..
just set the height of the ScrollView dynamically as per the Objects added to the ScrollView....
No need to set the predefined height for this...take the dynamic height....
scrollview.contentSize = CGSizeMake(320, txtView.contentSize.height+450);
CGPoint topOffset = CGPointMake(0,0);
[scrollview setContentOffset:topOffset animated:YES];
Hope this will surely work for you..
Good Luck :)
Checkout the sample code called 'Tiling'. It might be what you're looking for.
When scrolling stops, couldn't you just adjust the bounds so that you are now "re-centered" with your new offset?
Presumably you can't scroll more than so many pixels before your finger hits the edge of the screen, so you only need bounds that a few hundred pixels outside the original center.
Once you have a new center, adjust your bounds accordingly, retiling as necessary.

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 bounces when content is smaller than view's size

How can I simply let the scrollview bounce, when the content size is smaller than the view's frame and when I drag it?
like iPhone's app search results.
see UIScrollView (paging mode) bounces only when there two or more pages?
you'd better set the property:
scroll.alwaysBounceVertical = YES;
scroll.alwaysBounceHorizontal = YES;
answer myself: the simplest way would be to set the height of the content size to be the height of the frame plus 1. kind of stupid but according to document it only scrolls when size is bigger than frame.
If you prefer .xib just check Bounce Vertically.

iPhone Horizontal Scrolling

I am trying to create an app with horizontal scrolling, so that one would be able to scroll horizontally through a series of images. I watched the WWDC Session 104 video on this, and while they made an interesting app, they flew through the basics of it very quickly.
I understand using the UIScrollView, and that I have to enable paging. After that they say that I should add more views as subviews of the scrollview, but I am not clear on how to do that. I am also not clear on how I add my images to those views.
As you can probably tell I am pretty new at this so any help would be appreciated.
You want to look into UIImageView. It's a view specifically for holding images.
When you add your images, you want to set their rects (probably using initWithFrame: for each UIImageView) so that:
the first image is at 0,0
the second image is at 320,0
third is at 640,0 (etc)
I.e. each image is 320 pixels right of the previous.
The final step is to set the contentSize for your UIScrollView -- this is a CGSize which describes the total size of the scroll view.
If you have 3 images, you would then set it to (320*3) * 480 using e.g.
myScrollView.contentSize = CGSizeMake(320*3, 480);
A lot of people, when they initialize the scroll view, have a for loop or similar which steps through the images they want to display. These for loops tend to look something like this:
CGFloat scrollWidth = 0.f;
for (UIImage *someImage in someNSArrayWithImages) {
UIImageView *theView = [[UIImageView alloc] initWithFrame:
CGRectMake(scrollWidth, 0, 320.f, 480.f)];
theView.image = someImage;
[myScrollView addSubview:theView];
[theView release];
scrollWidth += 320.f;
}
myScrollView.contentSize = CGSizeMake(scrollWidth, 480.f);
This way you'll get things lined up and you'll get the content size for you at the same time.
If you want to make it so that the scroll view "intelligently" scrolls to each image and stops when people slide left/right, you can do myScrollView.pagingEnabled = YES.
Hope that helps get you going.
Assuming you have "infinite" images, putting them all there at or before launch time in a huge UIScrollView will not be an option. (there is a limit to the size of a UIView)
The way I solved it: Make a UIScrollView covering the whole screen. It's content should be a UIView of 3*320 width and 480 height, extending 320px left and 320px right.
Put 3 UIImageView's in it, left, middle and right. Set paging=YES, so the uiscrollview clips to the 3 "pages" you've created.
Make sure your class is the delegate of the uiscrollview, and listen for
-(void)scrollViewDidEndDragging:(UIScrollView*)sv willDecelerate:(BOOL)notFinished
-(void)scrollViewDidEndDecelerating:(UIScrollView*)sv
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView*)sv
and make the appropriate transitions on hitting paging boundaries; shift images and set ContentOffset so you're looking at the center image again.
I suggest you make this first, and only then read on...
Then you will hit on a bug, documented here UIScrollView - (bounces = NO) seems to override (pagingEnabled = YES) and here http://www.iphonedevsdk.com/forum/iphone-sdk-development/935-paging-uiscrollview.html, which makes that you cannot disable bouncing and have paging enabled at the same time. So enable bouncing, and subclass UIScrollView, overruling setContentOffset in there to prevent bouncing. (having bouncing really enabled will make for a rather unusual user experience)
Have a look at Apple's PageControl sample code. It's fairly short and easy to follow so you'll get the gist of setting up a project where multiple view controllers are loaded as you swipe horizontally.
Once you have this setup then it's the view controller's responsibility to load its own content (in your case, an image). You should make sure you understand how to load images first (using threads, etc) before you tackle paging, etc.
Think of it as two independent tasks. The view control is responsible for loading and displaying an image. The scroll view with paging just tells the appropriate view controller when to load itself (it doesn't care what the view controller does once its loaded)
Good luck!