Zoom UIScrollView with multiple images - iphone

I have a UIScrollFrame that contains multiple UIImageViews.
What is the correct way of supporting zoom in such a control?
Which view should 'viewForZoomingInScrollView' return?

I finally implemented it by putting a scrollview within a scrollview.
This way, the inner view handles page swipes while the outer handles zoom.

Apple has a really nice piece of Sample Code that is all about UIScrollView and zooming and tiling. Check it out at
http://developer.apple.com/iphone/library/samplecode/ScrollViewSuite/index.html
This code is discussed in the Scoll Views session of WWDC2009. The video of that session is unfortunately not freely available, but you can buy it as part of the iPhone WWDC2009 video package.

Just put a scrollview within another.
Like that:
-(void)zoomToSelectedImage:(NSIndexPath *)indexPath
{
int currentPage = indexPath.row;
//The External Scrollview. It move back and forward
UIScrollView *contentViewZoom = [[UIScrollView alloc] initWithFrame:self.frame];
[contentViewZoom setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.8]];
[contentViewZoom setContentSize:CGSizeMake(contentViewZoom.frame.size.width * [arrayOfImages count], contentViewZoom.frame.size.height)];
[contentViewZoom setPagingEnabled:YES];
[contentViewZoom setDelegate:self];
for (int i = 0; i < [arrayOfImages count]; i++) {
CGRect frame;
frame.origin.x = contentViewZoom.frame.size.width * i;
frame.origin.y = 0;
frame.size = contentViewZoom.frame.size;
//The Internal Scrollview. It allow you zoom the picture
UIScrollView *imageScrollView = [[UIScrollView alloc]initWithFrame:frame];
[imageScrollView setTag:1];
[imageScrollView setMinimumZoomScale:1.0];
[imageScrollView setMaximumZoomScale:3.0];
[imageScrollView setAlpha:1];
[imageScrollView setDelegate:self];
[imageScrollView setBouncesZoom:YES];
[imageScrollView setClipsToBounds:YES];
[imageScrollView setShowsHorizontalScrollIndicator:NO];
[imageScrollView setShowsVerticalScrollIndicator:NO];
[imageScrollView setContentSize:CGSizeMake(768, 1024)];
UIImage *image = [arrayOfImages objectAtIndex:i];
UIImageView* imgView = [[UIImageView alloc] init];
[imgView setImage:image];
[imgView setFrame:CGRectMake(0, 0, 768, 1024)];
imgView.bounds = self.bounds;
[imgView setClipsToBounds:YES];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[imageScrollView addSubview:imgView];
[contentViewZoom addSubview:imageScrollView];
}
[self addSubview:contentViewZoom];
[contentViewZoom setAlpha:0.2];
//Positioning according to current position
CGRect frame;
frame.origin.x = contentViewZoom.frame.size.width * currentPage;
frame.origin.y = 0;
frame.size = contentViewZoom.frame.size;
[contentViewZoom scrollRectToVisible:frame animated:NO];
}
OBS.: In my case, the screen size is fixed (768x1024), you should change as you need.

Related

How to add images to UIPageViewController?

I have a UIPageViewController with 5 screens. Please see the below image
I need to add some images instead of the label Screen #1, Screen #2, etc.
Here is my code
Can anyone please tell me the solution?
Try this code..
Add whatever yor images in your imageArray array;
NSArray *imageArray=[[NSArray alloc]initWithObjects:#"SpaceTheme.png",#"BeachTheme.png",#"vintage.png", nil];
ScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320,480)];
[Scroll setPagingEnabled:YES];
[Scroll setShowsHorizontalScrollIndicator:NO];
for(n=0; n< [imageArray count];n++)
{
NSString *imageName=[imageArray objectAtIndex:n];
NSString *fullImageName=[NSString stringWithFormat:#"%#",imageName];
CGRect imageViewFrame;
int padding=20;
imageViewFrame=CGRectMake(Scroll.frame.size.width*n+padding-10,Scroll.frame.origin.y-130,Scroll.frame.size.width-1*padding, Scroll.frame.size.height);
UIImageView *imageView=[[UIImageView alloc]initWithFrame:imageViewFrame];
[imageView setImage:[UIImage imageNamed:fullImageName]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[Scroll addSubview:imageView];
pagecontrol.numberOfPages = 3; // Give numbre of pages here
pagecontrol.currentPage = 0;
[self.view addSubview:pagecontrol];
}
Scroll.delegate=(id)self;
CGSize scrollViewSize=CGSizeMake(Scroll.frame.size.width*[imageArray count], Scroll.frame.size.height);
[Scroll setContentSize:scrollViewSize];
[self.view addSubview:Scroll];

Getting Image index

I am developing an app in that i have 15 images for stored in an array as front images and another 15 images as back image. I want to add that image to scrollview vertically i have done that successfully but now my problem is that how can I compare that two array images.
Adding front images on vertical scrollview added succesfully but not randomly shuffled, ie when I double tap on the front image the back array image is displayed but still front image present in that imageview.
Please help me out to solve this problem.
Thanks in advance.
Please check my code :
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
delegate.front=TRUE;
delegate.back=FALSE;
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[scrollView setPagingEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:NO];
FrontsCards=[[NSMutableArray alloc]initWithObjects:#"cloub1.png",#"cloub2.png",#"cloub3.png",#"cloub4.png",#"cloub5.png",#"cloub6.png",#"cloub7.png",#"cloub8.png",#"cloub9.png",#"cloub10.png",#"cloub11.png",#"cloub12.png",#"diamond1.png",#"diamond2.png",#"diamond3.png",#"diamond4.png",#"diamond5.png", nil];
for(int m=0; m<[FrontsCards count];m++)
{
ImgView.tag=m;
int randIdx=arc4random()%[FrontsCards count];
NSString *imageName=[FrontsCards objectAtIndex:randIdx];
// NSLog(#"%d",randIdx);
NSString *fullImageName=[NSString stringWithFormat:#"%#",imageName];
int padding=25;
// padding is given.
CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);
ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];
[ImgView setImage:[UIImage imageNamed:fullImageName]];
[ImgView setContentMode:UIViewContentModeScaleAspectFill];
[scrollView addSubview:ImgView];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapImgView:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.delegate = self;
[self.ImgView addGestureRecognizer:doubleTap];
self.ImgView.userInteractionEnabled=YES;
}
CGSize scrollViewSize=CGSizeMake(scrollView.frame.size.width*[FrontsCards count], scrollView.frame.size.height);
[scrollView setContentSize:scrollViewSize];
[self.view addSubview:scrollView];
}
- (void)doubleTapImgView:(UITapGestureRecognizer *)gesture
{
AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
delegate.back=TRUE;
delegate.front=FALSE;
NSLog(#"%d", gesture.view.tag);
NSLog(#"double-tap");
BackCards =[[NSMutableArray alloc]initWithObjects:#"card1.jpg",#"card2.jpg",#"card3.jpg",#"card4.jpg", nil];
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[scrollView setPagingEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:NO];
for(int m=0; m< [BackCards count];m++)
{
NSString *imageName=[BackCards objectAtIndex:m];
NSString *fullImageName=[NSString stringWithFormat:#"%#",imageName];
int padding=25;
// padding is given.
CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);
ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];
[ImgView setImage:[UIImage imageNamed:fullImageName]];
[ImgView setContentMode:UIViewContentModeScaleAspectFill];
[scrollView addSubview:ImgView];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapImgView:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.delegate = self;
[self.ImgView addGestureRecognizer:doubleTap];
self.ImgView.userInteractionEnabled=YES;
}
CGSize scrollViewSize=CGSizeMake(scrollView.frame.size.width*[FrontsCards count], scrollView.frame.size.height);
[scrollView setContentSize:scrollViewSize];
[self.view addSubview:scrollView];
}
For achieving this one you can set Tag value for every image when you tap on that particuler image you'l get tag value and use that tag value.
self.imagview.tag=Unique_tagValue;
when click on that image you can set image from another Array.
self.imageview.image=[UIImage imageNamed:[backArray objectAtIndex:self.imageview.tag]];
EDIT:-
in viewDidLoad method set tags for imageview like this, ImgView.tag=m+100;
keep below code in doubleTapImgView method
for(int m=0; m< [BackCards count];m++)
{
UIImageView *imgview=(UIImageView *)[scrollview viewWithTag:m+100];
imgview.image=[UIImage imagNamed:[BackCards objectAtIndex:m]];
}
Subview Translation:-
[self.view bringSubviewToFront:frontview];
Either you can use the same name for the images in backArray and front Array or you can set the tag value of each image uniquely and compares that tags to solve the problem.
imgV.tag=tagValue;
Hope this helps.

Not able to see images inside scrollview

I have 2 scrollviews and 1 imageview.
now Inside Main scrollview I have another scrollview and in this scrollview I have image.
so, my Main Scrollview named "ScrollView" and sub scrollview named "scrView"
now I am able to add "scrView" inside "ScrollView" but I can not see my image inside "scrView"
My coding is as follows:
int numberOfPages = 10;
[scrollView setPagingEnabled:YES];
[scrollView setContentSize:CGSizeMake(scrollView.bounds.size.width * numberOfPages, scrollView.bounds.size.height)];
CGRect pageFrame;
CGRect imageFrame;
UIImageView *imageView;
for(int i=0; i < numberOfPages; i++)
{
pageFrame = CGRectMake(i * scrollView.bounds.size.width+50, 0.0f, scrollView.bounds.size.width-100, scrollView.bounds.size.height);
UIScrollView *scrView = [[UIScrollView alloc] init];
imageFrame = CGRectMake(i * scrView.bounds.size.width, 0.0f, scrView.bounds.size.width, scrView.bounds.size.height);
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.jpg"]];
[scrView setFrame:pageFrame];
[imageView setFrame:imageFrame];
[scrView setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:scrView];
[scrView addSubview:imageView];
[imageView release];
}
If you look at the code I have added imageView as a subView of scrView but I am not able to see image in simulator.
what is the problem?
share your ideas with me.
thanks...
This is working try this code.Problem was with your imageview frame
int numberOfPages = 10;
[scrollView setPagingEnabled:YES];
[scrollView setContentSize:CGSizeMake(scrollView.bounds.size.width * numberOfPages, scrollView.bounds.size.height)];
CGRect pageFrame;
CGRect imageFrame;
UIImageView *imageView;
for(int i=0; i < numberOfPages; i++)
{
pageFrame = CGRectMake(i * scrollView.bounds.size.width+50, 0.0f, scrollView.bounds.size.width-100, scrollView.bounds.size.height);
UIScrollView *scrView = [[UIScrollView alloc] initWithFrame:pageFrame];
imageFrame = CGRectMake(0.0, 0.0f, scrView.bounds.size.width, scrView.bounds.size.height);
imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.image=[UIImage imageNamed:#"image1.jpg"];
// [scrView setFrame:pageFrame];
[scrView setBackgroundColor:[UIColor redColor]];
[scrView addSubview:imageView];
[scrollView addSubview:scrView];
[imageView release];
}
Set a background color to your imageView too.. Then Log and check the scrView & imageFrame frames. Also use bringSubviewToFront: property.
for(int i=0; i < numberOfPages; i++)
{
UIScrollView *scrView = [[UIScrollView alloc] init];
[scrView setBackgroundColor:[UIColor redColor]];
pageFrame = CGRectMake(i * scrollView.bounds.size.width+50, 0.0f, scrollView.bounds.size.width-100, scrollView.bounds.size.height);
imageFrame = CGRectMake(i * scrView.bounds.size.width, 0.0f, scrView.bounds.size.width, scrView.bounds.size.height);
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.jpg"]];
[scrView setFrame:pageFrame];
[imageView setFrame:imageFrame];
[imageView setBackgroundColor:[UIColor blueColor]]
[scrView addSubview:imageView];
[scrView bringSubviewToFront:imageView];
[imageView release];
[scrollView addSubview:scrView];
[scrollView bringSubviewToFront:scrView];
}
just try to set like this from your code mate..
[scrView setBackgroundColor:[UIColor clearColor]];
[scrView addSubview:imageView]; /// first add imageview and then scrview (screen view) to scrollview
[scrollView addSubview:scrView];

How to combine UIScrollview with UIPagecontrol to show different views?

I've searched and searched for a tutorial for this but none of them are what I'm looking for. I've tried Apple's sample but it is just colors and I don't know how to make it views. All I'm looking for is a screen that will page while showing the page control. Each time the scroll view pages i want it to show a completely different view with buttons, a lot like the home screen of the iPhone. I found the sample code below which works very well with just images, but I'd like to modify to work with separate views. Please Help! Thank you.
- (void)setupPage {
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor clearColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = image.size.height;
rect.size.width = image.size.width;
rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
self.pageControl.numberOfPages = nimages;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
I was experimenting with this just the other day. I'm still getting used to using a UIScrollView but here's how you can add views to your UIScrollView:
UIView *blueView = [[UIView alloc] init];
blueView.frame = CGRectMake(100, 0, 500, 1024);
blueView.backgroundColor = [UIColor colorWithRed:164.0/256 green:176.0/256 blue:224.0/256 alpha:1];
[scrollView addSubview:blueView];
[blueView release];
UIView *orangeView = [[UIView alloc] init];
orangeView.frame = CGRectMake(700, 0, 500, 1024);
orangeView.backgroundColor = [UIColor colorWithRed:252.0/256 green:196.0/256 blue:131.1/256 alpha:1];
[scrollView addSubview:orangeView];
[orangeView release];
Notice that I'm setting the x value in frame.origin of each view so that they're sitting adjacent to each other. You also have to set the content size of the UIScrollView with something like [scrollView setContentSize:CGSizeMake(1200, 1024)]; so that it knows how big its subviews are.
Then, if you need to control a UIPageControl, you would set its numberOfPages to 2 (for the example scrollview above) and change its currentPage property. You could do this by implementing scrollViewDidEndDecelerating:, which is a method in the UIScrollViewDelegate. You could check which "page" the scrollview is at by checking its contentOffset.x value.
Hope this helps!
Here's the Apple PageControl code with images. To use, just drag your images to the project. The default images are image1.jpg, image2.jpg, etc. If you need png, just change the extension to .png.
Then replace the MyViewController.m code with this code, and you've got pagecontrol with Images.
Cheers, Jordan
http://pastebin.com/raw.php?i=c3hS29sC
// Adding UIView to PageControl example
- (id)initWithPageNumber:(int)page {
UIScreen *screen = [UIScreen mainScreen];
CGRect frame = [screen applicationFrame];
frame.origin.y=0;
if (self = [super initWithNibName:#"MyView" bundle:nil]) {
UIView *view = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:view];
// Add whatever you want to the view here.
[view release];
pageNumber = page;
}
return self;
}
Here's another answer...
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"Views" owner:self options:nil];
infoView = [nibViews objectAtIndex:0];
[self.view addSubview:infoView];
Then you have your view.
Cheers

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];