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];
Related
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.
I am developing an app where I add images to an NSMutableArray and display them in an image view.
My problem is that I will don't know how to get the index of the selected or tapped image in my app.
FrontsCards=[[NSMutableArray alloc]initWithObjects:#"cloub1.png",#"cloub2.png",#"cloub3.png",#"cloub4.png", nil];
for(int m=0; m< [FrontsCards count];m++)
{
NSString *imageName=[FrontsCards objectAtIndex:m];
NSString *fullImageName=[NSString stringWithFormat:#"%#",imageName];
int padding=25;
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];
What should I do in my tap gesture recognizer to get the index of the image?
- (void)doubleTapImgView:(UITapGestureRecognizer *)gesture
{
NSLog(#"double-tap");
}
set a tag to each imageView you using like this :
ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];
ImgView.tag = m;
and then replace this method:
- (void)doubleTapImgView:(UITapGestureRecognizer *)gesture
{
NSLog(#"double-tap");
NSLog(#"%d", gesture.view.tag);
}
it will print you the index of the image in the imageView
maybe you can use something like this:
first add the name of the image as the accessibilityIdentifier of the the imageview
[imgView setAccessibilityIdentifier:imageName];
then in the tapRecognizer:
-(void)doubleTapImgView:(UITapGestureRecognizer *)gesture{
UIImageView *imgView = (UIImageView *)gesture.view;
int idx = [FrontCards indexOfObject:[imgView accessibilityIdentifier]];
}
I have an application in which a pickerview is ther with images.I had done that with this.`
picker1=[[UIPickerView alloc] init];
picker1.frame= CGRectMake(30,90,155,316);
picker1.delegate = self;
picker1.dataSource = self;
picker1.showsSelectionIndicator = YES;
picker1.tag = 1;
[self.view addsubview:picker1];
and i am using viewForRow delegate methode to load the images.i need to increase the images frame there inside the pickerview.`
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%#.png",[self.array objectAtIndex:row]]];
UIImageView *icon = [[UIImageView alloc] initWithImage:img];
//temp.frame = CGRectMake(170, 0, 30, 30);
UIView *tmpView = [[[UIView alloc] initWithFrame:CGRectMake(155,75,95,35)] autorelease];
[tmpView insertSubview:icon atIndex:0];
picker1.backgroundColor = [UIColor clearColor];
[(UIView*)[[picker1 subviews] objectAtIndex:2] setAlpha:0.0f];
[(UIView*)[[picker1 subviews] objectAtIndex:0] setAlpha:0.0f];
[(UIView*)[[picker1 subviews] objectAtIndex:1] setAlpha:0.0f];
[(UIView*)[[picker1 subviews] objectAtIndex:3] setAlpha:0.0f];
[tmpView setUserInteractionEnabled:NO];
[tmpView setBackgroundColor:[UIColor clearColor]];
[tmpView setTag:row];
return tmpView;
but when i try to increase the frame of the view its position only changing,not the size.can anybody help me to achieve this?
implement the following pickerview delegate method..
- (CGSize)rowSizeForComponent:(NSInteger)component{
CGSize size = CGSizeMake(320, 60); //input ur custom size
return size;
}
I use the following to display scrollview and pagecontrol
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 179)];
pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(0, 179, 320, 20)];
scrollView.delegate = self;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
int index=[[self._parameters objectForKey:#"index"] intValue];
NSString *imageNamePrefix=[[activeAppIdList objectAtIndex:index] objectForKey:#"AppID"];
int noOfScreenShot=[[[activeAppIdList objectAtIndex:index] objectForKey:#"NoOfScreenShot"] intValue];
CGFloat cx = 0;
for (int j=1;j<=noOfScreenShot ; j++) {
UIImage *image =[[UIImage alloc] init];
image= [self loadImage:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
if (image == nil) {
NSString *urlString = [[NSString alloc]initWithFormat:#"http://localhost/MoreApp/images/%#_%d.jpg",imageNamePrefix,j];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *temp=[[UIImage alloc]initWithData:imageData] ;
[self saveImage:temp withName:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
image = temp;
[temp release];
[urlString release];
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[image release];
imageView.frame = CGRectMake(cx,0,scrollView.frame.size.width,scrollView.frame.size.height);
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
NSLog(#"No Of pages..%d",noOfScreenShot);
pageControl.numberOfPages = noOfScreenShot;
pageControl.currentPage = 0;
scrollView.contentSize = CGSizeMake(cx,scrollView.frame.size.height);
NSLog(#"Width:%f",scrollView.frame.size.width);
[ refToSelf.instructionView addSubview:scrollView];
[ refToSelf.instructionView addSubview:pageControl];
[scrollView release];
[pageControl release];
scrollview is ok but pagecontroll didn't showed...
can help??
If the page control's and container's background color is the same (by default white) page control won't be visible.
For anyone who finds this, another silly mistake you can make is to not set the numberOfPages attribute on the UIPageControl, which will also cause it to not display.
My mistake was equally annoying - developing with storyboard in 3.5 inch view, and built on 4 inch device on simulator so it was offscreen. Silly!
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.