Pagination in scrollview - iphone

_
viewdidload
{
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
i am using this code for displaying pagination in scrollview with different colors,for identification,i just want to replace it with images instead of colors.i am using this code
NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:#"h1#2x"], [UIImage imageNamed:#"h2#2x"], [UIImage imageNamed:#"h2.1#2x"], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView*subview = [[UIView alloc] initWithFrame:frame];
UIImage *imggg = [colors objectAtIndex:i];
[subview setBackgroundColor:[UIColor colorWithPatternImage:imggg]];
[self.scrollView addSubview:subview];
[subview release];
}
But i get only two view, and the image is not size to fit it is delocated.How to set images with the above code.Please help me to solve this problem.
Thanks in advance.

I got the answer by reducing the size of the images in the array to exactly the size of the scrollview. My scrollview size is 320,424, and I slice the size of the images to 320,424. It works perfectly. Thanks.

UIView *subview = [[[UIView alloc] initWithFrame:frame] autorelease];
[subview setBackgroundColor:[colors objectAtIndex:i]];
[[self scrollView] addSubview:subview];
this looks like what your code is trying to do...
If you want images on each page though:
UIImage *img = [UIImage imageNamed:#"nameOfImageFile"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
[[self scrollView] addSubview:imageView];
to have multiple images for each page, just use an array of UIImages for the page.
NSArray *imageNames = [NSArray arrayWithObjects:#"image1Name", #"image2name", #"image3Name", nil];
UIImage *img = [UIImage imageNamed:[imageNames objectAtIndex:pageNumber]];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
[[self scrollView] addSubview:imageView];

#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIScrollView *scrollView;
}
#end
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:#"chiranjeevi.jpeg"], [UIImage imageNamed:#"AR.jpeg"], [UIImage imageNamed:#"sachin.jpeg"], nil];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*colors.count,scrollView.frame.size.height);
for (int i = 0; i <[colors count]; i++)
{
UIImage *imggg = [colors objectAtIndex:i];
CGRect frame;
frame.origin.x =scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image=imggg;
[scrollView addSubview:subview];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end

Related

Pinch zoom not working in UIScrollview for Multiple image in ios7

I want to pinch zoom in uiscrollview image. i added multiple image in uiscrollview
but pinch zoom is not working i want to pinch zoom UIImageview image. i have refer so many reference but still its not working
Thanks in advance
My code:-
for(int i = 0;i<aryImage.count;i++)
{
//Create a uiimageview
imageView =[[UIImageView alloc]init ];
imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
imageView.image = [UIImage imageNamed:#"default.png"];
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[scrollGallery addSubview:imageView];
scrollGallery.minimumZoomScale = 1.0;
scrollGallery.maximumZoomScale = 2.0;
scrollGallery.delegate = self;
[scrollGallery addSubview:imageView];
}
scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);
pageControl.numberOfPages = aryImage.count;
pageControlBeingUsed = NO;
pageControl.currentPage = 0;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
Try this:
- (void)scrollViewDidZoom:(UIScrollView *)sv
{
UIView* zoomView = [sv.delegate viewForZoomingInScrollView:sv];
CGRect zvf = zoomView.frame;
if(zvf.size.width < sv.bounds.size.width)
{
zvf.origin.x = (sv.bounds.size.width - zvf.size.width) / 2.0;
}
else
{
zvf.origin.x = 0.0;
}
if(zvf.size.height < sv.bounds.size.height)
{
zvf.origin.y = (sv.bounds.size.height - zvf.size.height) / 2.0;
}
else
{
zvf.origin.y = 0.0;
}
zoomView.frame = zvf;
}
You return only one imageView in viewForZooming but you added an array of them. This may be the cause of your problem. Try to replace your code like this:
UIView *containerView = [[UIView alloc] initWithFrame:scrollGallery.frame];
[scrollGallery addSubview:containerView];
scrollGallery.minimumZoomScale = 1.0;
scrollGallery.maximumZoomScale = 2.0;
scrollGallery.delegate = self;
for(int i = 0;i<aryImage.count;i++)
{
//Create a uiimageview
imageView =[[UIImageView alloc]init ];
imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
imageView.image = [UIImage imageNamed:#"default.png"];
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[containerView addSubview:imageView];
}
scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);
// then maybe you need to resize containerView's frame
//..........
pageControl.numberOfPages = aryImage.count;
pageControlBeingUsed = NO;
pageControl.currentPage = 0;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return containerView;
}
Hope it will help.
UPD: also try to disable userInteraction of your imageViews. They may block interaction with scrollGallery.
UPD2: store all imageViews in predefined array. For, example,
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i=0; i<aryImage.count; i++) {
[arr addObject:[[UIImageView alloc] init]];
}
Then in your cycle instead of
imageView =[[UIImageView alloc] init];
use
UIImageView *imageView = [arr objectAtIndex:i];
And don't forget to release everything at the end ;)

Scrolling of Images using UIScrollView and ImageView in iPhone

I am working one of the app, In that app,there is only one view that has an image on it. I want to be able to swipe left or right and have the first image go out of the view and the second image to come in. The images are different and I want it to look like they are connected. I have used UIScrollView and UIImageView on xib files below is my code which i am using in view did load method. When i run the app, only last image is showing in scrollview.
NSMutableArray *images = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"iPad.png"],[UIImage imageNamed:#"iPadMini.png"],[UIImage imageNamed:#"iPhone4.png"],[UIImage imageNamed:#"iPhone5.png"], nil];
for (int i = 0; i < [images count]; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
imgView.frame = frame;
imgView.image = [images objectAtIndex:i];
[scrollView addSubview:imgView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
Try with this block of code...
NSMutableArray *images = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"iPad.png"],[UIImage imageNamed:#"iPadMini.png"],[UIImage imageNamed:#"iPhone4.png"],[UIImage imageNamed:#"iPhone5.png"], nil];
for (int i = 0; i < [images count]; i++)
{
UIImageView *imgView = [[UIImageView alloc] init];
imgView.frame = CGRectMake(100*i, 0, 100, self.scrollView.frame.size.height);
imgView.image = [images objectAtIndex:i];
[scrollView addSubview:imgView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
Try this..
NSMutableArray *images = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"iPad.png"],[UIImage imageNamed:#"iPadMini.png"],[UIImage imageNamed:#"iPhone4.png"],[UIImage imageNamed:#"iPhone5.png"], nil];
for (int i = 0; i < [images count]; i++) {
UIImageView *imgView = [[UIImageView alloc]init];
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
imgView.frame = frame;
imgView.image = [images objectAtIndex:i];
[scrollView addSubview:imgView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
Hope it helps...

Array of Images display in UIScrollview and Paging successfully but cannot properly zoom-in images in scrollview

I am working on UIScrollview with ImageArray. Scrolling and paging are working but I not able to zoom each image. My code of image Scrollview is below :-
#define IMAGE_WIDTH 320
#define IMAGE_HEIGHT 360
- (void)viewDidLoad
{
[super viewDidLoad];
// TODO – fill with your photos
NSArray *photos = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1m.jpg"],
[UIImage imageNamed:#"photo2m.jpg"],
[UIImage imageNamed:#"photo3m.jpg"],
[UIImage imageNamed:#"photo4m.jpg"],
nil] retain];
// note that the view contains a UIScrollView in aScrollView
int i=0;
for ( NSString *image in photos )
{
UIImage *images = [photos objectAtIndex:i];
imageView = [[UIImageView alloc] initWithImage:images];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
imageView.frame = CGRectMake( IMAGE_WIDTH * i++, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
[self.scrollViewimages addSubview:imageView];
[imageView release];
}
self.scrollViewimages.contentSize = CGSizeMake(IMAGE_WIDTH*i, IMAGE_HEIGHT);
self.scrollViewimages.delegate = self;
}
Need to help for implementing pinch zoom of every images. Please help !
for ( NSString *image in photos )
{
UIImage *images = [photos objectAtIndex:i];
imageView = [[UIImageView alloc] initWithImage:images];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
imageView.tag = 1;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake( IMAGE_WIDTH * i++, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];
scrollView.delegate = self;
scrollView.maximumZoomScale = 3.0f
imageView.frame = scrollView.bounds;
[scrollView addSubview:imageView];
[imageView release];
[self.scrollViewimages addSubview:scrollView];
}
And Implement the delegate method in UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView viewWithTag:1];
}
#define IMAGE_FOR_ZOOM_TAG (1)
Call following custom method
-(void) setupProductImageViewContainerUI
{
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
if(self.dimView)
{
for(UIView *subView in self.dimView.subviews)
[subView removeFromSuperview];
[self.dimView removeFromSuperview]; self.dimView = nil;
}
self.dimView = [[UIView alloc] initWithFrame:window.bounds];
self.dimView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];
self.dimView.userInteractionEnabled = YES;
[window addSubview:self.dimView];
self.imgContainerView = [[UIView alloc] init];
self.imgContainerView.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
self.imgContainerView.backgroundColor = [UIColor whiteColor];
[self.dimView addSubview:self.imgContainerView];
UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:window.bounds];
mainScrollView.pagingEnabled = YES;
mainScrollView.delegate = self;
mainScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.showsVerticalScrollIndicator = NO;
[self.imgContainerView addSubview:mainScrollView];
CGRect innerScrollFrame = mainScrollView.bounds;
IndexOfSlidingPhoto = 0;
for(int i = 0 ; i < listOfImages.count; i++)
{
UIImage *imgProduct = [GeneralClass getImageForSreenFromScreenTable:#"" orCustomTable:#"" OrDefaultImga:[listOfImages objectAtIndex:i]];
UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:imgProduct];
imageForZooming.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
imageForZooming.tag = IMAGE_FOR_ZOOM_TAG;
imageForZooming.contentMode = UIViewContentModeScaleAspectFit;
UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
pageScrollView.minimumZoomScale = 1.0f;
pageScrollView.maximumZoomScale = 20;
pageScrollView.zoomScale = 1.0f;
pageScrollView.contentSize = imageForZooming.bounds.size;
pageScrollView.delegate = self;
pageScrollView.showsHorizontalScrollIndicator = NO;
pageScrollView.showsVerticalScrollIndicator = NO;
[pageScrollView addSubview:imageForZooming];
[mainScrollView addSubview:pageScrollView];
if (i < listOfImages.count -1)
innerScrollFrame.origin.x += innerScrollFrame.size.width;
mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height);
[window bringSubviewToFront:self.dimView];
}
#pragma Mark -
#pragma Mark - UIScrollView Delegate Methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView viewWithTag:IMAGE_FOR_ZOOM_TAG];
}

how to do infinite scrolling images using cocoa touch

I want to add infinite scrolling images and there is no end for images scrolling:
const CGFloat kScrollObjHeight = 200.0;
const CGFloat kScrollObjWidth = 320.0;
const NSUInteger kNumImages = 17;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollview1 subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
[scrollview1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollview1 bounds].size.height)];
}
-(void)viewDidLoad
{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
[scrollview1 setBackgroundColor:[UIColor blackColor]];
[scrollview1 setCanCancelContentTouches:NO];
scrollview1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollview1.clipsToBounds = YES;
scrollview1.scrollEnabled = YES;
scrollview1.pagingEnabled = YES;
for(int i=0;i< kNumImages;i++)
{
NSString *imgName=[[NSString alloc] initWithFormat:#"head%d.png",i];
UIImage *img=[UIImage imageNamed:imgName];
UIImageView *imageView=[[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(i* kScrollObjWidth, 0, kScrollObjWidth,kScrollObjHeight)];
[scrollview1 addSubview:imageView];
[imageView release];
[imgName release];
}
scrollview1.contentSize= CGSizeMake(kScrollObjWidth* kNumImages, kScrollObjHeight);
[self layoutScrollImages];
[super viewDidLoad];
}
Just make a simple new project with .h file as
#import <UIKit/UIKit.h>
#interface scrollableImageViewController : UIViewController {
IBOutlet UIScrollView *sview;
}
#end
and the .m file with this method
- (void)viewDidLoad {
[super viewDidLoad];
for(int i=0;i<10;i++)
{
NSString *name=[[NSString alloc] initWithFormat:#"images%d.png",i];
NSLog(#"%#",name);
[name release];
UIImage *img=[UIImage imageNamed:#"img.png"];
UIImageView *imageView=[[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(i*57, 0, 57, 57)];
[sview addSubview:imageView];
[imageView release];
}
sview.contentSize= CGSizeMake(57*10, 57);
}
and check the result
You can check out Apples sample code PhotoScroller.
It's the code they introduced at WWDC 11
in Session 104 - Advanced ScrollView Techniques. You can watch this video (and the corresponding PDF) for free if you are a registered Apple developer.

doesn't work correctly. addSubview problem

Help me.
addSubview doesn't work.
I want to add "ContentView" on scrollView.
But "ContentView" doesn't appear on screen.
"ContentView" is UIView.
When I change to self.view=scrollView from [self.view addSubview:scrollView],
addSubview doesn't work.
Please teach me how to add UIView on this screen!!
- (void)viewDidLoad {
[super viewDidLoad];
scrollViewMode = ScrollViewModeNotInitialized;
mode = 1;
imageViewArray = [[NSMutableArray alloc] init];
mainStatic = [[MainStatics alloc] init];
[mainStatic setSetting];
NSString* path = [[NSBundle mainBundle] pathForResource:#"Files" ofType:#"plist"];
NSArray* dataFiles = [NSArray arrayWithContentsOfFile:path];
kNumImages = [dataFiles count];
CGRect frame = [UIScreen mainScreen].applicationFrame;
scrollView = [[touchClass alloc] initWithFrame:frame];
scrollView.delegate = self;
scrollView.maximumZoomScale = 5.0f;
scrollView.minimumZoomScale = 1.0f;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setDelegate:self];
scrollView.delaysContentTouches=NO;
scrollView.userInteractionEnabled = YES;
[scrollView setCanCancelContentTouches:NO];
NSUInteger i;
for (i=0;i<[dataFiles count];i++)
{
UIImage* image = [UIImage imageNamed:[dataFiles objectAtIndex:i]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = NO;
CGRect rect = imageView.frame;
rect.size.height = 480;
rect.size.width = 320;
imageView.frame = rect;
imageView.tag = i+1;
[imageViewArray addObject:imageView];
}
self.view = scrollView;
[self setPagingMode];
UIView* contentView;
CGRect scRect = CGRectMake(0, 436, 320, 44);
contentView = [[UIView alloc] initWithFrame:scRect];
[contentView addSubview:toolView];
[self addSubview:contentView];
}
I used image array in following function.
toolView is UIView with UIToolbar.
So I want to add toolbar to screen.
Yes, touchClass is subclass of UIScrollView.
1.I see.I forgot release this.Thank you.
2.OK. I modified this.But "ContentView" didn't apear.
are there another reason?
- (void)setPagingMode {
CGSize pageSize = [self pageSize];
NSUInteger page = 0;
for (UIView *view in imageViewArray){
[scrollView addSubview:view];
view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
}
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = scrollView.showsHorizontalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(pageSize.width * [imageViewArray count], pageSize.height);
scrollView.contentOffset = CGPointMake(pageSize.width * currentPage, 0);
scrollViewMode = ScrollViewModePaging;
}
Source isn't clear.
You never use array with UIImageView. There is no info what is toolView etc...
Please provide more detailed source. Is touchClass a subclass of UIScrollView?
What I noticed in your code:
You don't release image and imageView. All this images imageViews will be leaked. At the end of loop you should add [imageView release]; and [image release];
I don't think that it is good idea to send [self addSubview:contentView]; Try to use [self.view addSubview:contentView];