scrollview sub uiscrollviews uiimageview viewForZoomingInScrollView - iphone

I have main UIScrollView
mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
mainScrollView.pagingEnabled = YES;
imageScrollView.contentSize = CGSizeMake(320 * [myImage count], 440);
, which contain many sub UIScrollView, and each sub UIScrollView contains UIImageView
for (int i = 0; i < [myImage count]; i++)
{
imageView = [[UIImageView alloc] init];
[imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
NSString *strimage = [myImage objectAtIndex:i];
NSURL *imageUrl = [NSURL URLWithString:strimage];
[imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:#"Placeholder"]];
imageView.frame = CGRectMake(0, 0, 320, 440);
subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, 440)];
subScrollView.contentSize =
CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
subScrollView.pagingEnabled = YES;
subScrollView.showsHorizontalScrollIndicator = NO;
subScrollView.showsVerticalScrollIndicator = NO;
subScrollView.delegate = self;
subScrollView.bouncesZoom = YES;
subScrollView.clipsToBounds = YES;
subScrollView.maximumZoomScale = 3.0;
subScrollView.minimumZoomScale = 1.0;
subScrollView.zoomScale = 1.0;
[mainScrollView addSubview:subScrollView];
[subScrollView addSubview:imageView];
}
, and I implement
#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
The problem is when I zoom in UIImageView, assume that there are 3 sub UIScrollView, the last sub UIScrollView can zoom UIImageView normally but the other 2 when I zoom in it , it zooms sub UIScrollView instead of UIImageView, and it effect the last sub UIScrollView to be also zoom, Did I do anything wrong?
I suspect that in viewForZoomingInScrollView, it's zoom only last UIImageView (last sub UIScrollView).
Thank you in advance :)

Finally I figured it out, I just need to update my UIImageView when I zoom in specific UIImageView
In - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView like this:
#pragma mark -
#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
for (UIImageView *iv in [scrollView subviews]) {
imageView = iv;
break;
}
return imageView;
}
Hope it may help someone, who struggle at this point as me, so you don't have to waste you time :).

Related

How to implement pinch gestures in iCarousel for zooming in and zooming out

I have implemented a gallery application by using iCarousel.Now I need to include pinch gestures to zoom in or zoom out each image.I added scroll view and tried the delegate methods.But no success.
please help me with this.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)indexs reusingView:(UIView *)view
{
imgview = [[UIImageView alloc] init];
UIImage * img = [items objectAtIndex:indexs];
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320.0f,568)];
((UIImageView *)view).image = [UIImage imageNamed:#"page.png"];
imgView.image= img ;
imgView.contentMode = UIViewContentModeScaleAspectFit;
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.backgroundColor = [UIColor redColor];
self.scrollView .frame = CGRectMake(18,10, 282, 568);
self.scrollView.delegate =self;
self.scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
self.scrollView.maximumZoomScale = MAXIMUM_SCALE;
self.scrollView.minimumZoomScale = MINIMUM_SCALE;
self.scrollView.clipsToBounds = NO;
[view addSubview:self.scrollView];
imgView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:imgView];
view.backgroundColor=[UIColor whiteColor];
view.layer.borderColor=[[UIColor lightGrayColor] CGColor];
view.layer.borderWidth=1;
view.layer.cornerRadius = 5;
view.layer.masksToBounds = YES;
return view;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imgView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
}
I have tried something like this.
And I tried adding pinch gestures to imgView also.

Zoom only a selected subview from the UIScrollView

I am adding many UIImageView's to UIScrollView,with paging enabled.I need to zoom only the image that i have tapped to zoom,rather than zooming the entire scrollview.Also zooming a particular image doesnot scale other subviews.
- (void)loadScrollViewWithImages {
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * imageList.count, self.view.bounds.size.height);
scrollView.pagingEnabled = YES;
UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];
CGFloat xPos = 0.0;
for (UIView *subView in scrollView.subviews) {
[subView removeFromSuperview];
}
int i = 0;
for (NSDictionary *imageDetails in self.imageList) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, 0.0, self.view.bounds.size.width, self.view.frame.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[imageView setClipsToBounds:YES];
[mainView addSubview:imageView];
xPos += self.view.bounds.size.width;
[imageView setImageWithURL:[NSURL URLWithString:[imageDetails objectForKey:#"media_path"]]];
[self.imageViewArray addObject:imageView];
if(self.selectedImageIndex == i) {
self.selectedImageView = imageView;
}
i++;
}
[mainView setFrame:CGRectMake(mainView.frame.origin.x, mainView.frame.origin.x,self.view.bounds.size.width * imageList.count, self.view.bounds.size.height)];
[scrollView addSubview:mainView];
}
I wanted to zoom only the selected index,but other subviews also add to the screen with no scaling.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [[[scrollView.subviews objectAtIndex:0] subviews] objectAtIndex:0] ;;
}
for implementing this you have to take a main scrollview as you have taken and instead of displaying imageView in this scrollview you have to take another scrollview and display the image and when user tries to zoom the image then only the selected scrollview gets scrolled.

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.

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