How to show multiple images inside UIScrollView in iOS

I am creating one application for the iPhone in which I need to show some images inside the UIScrollView, there will be two buttons on the left and right that the user can click on the button and display the next or previous image. Also at the bottom there is one button, when the user clicks on this button, it will show the image that we selected in scrollview. I would like to know how we show several images inside this scroll and when choosing the bottom button, how to find which image was inside scrollview .

+6
source share
5 answers

Example from danielbeard.wordpress.com

If your image names are not just numbers:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; [scrollView setPagingEnabled:YES]; [scrollView setAlwaysBounceVertical:NO]; NSArray *imagesArray = [NSArray arrayWithObjects:@"img1.png", @"img2.png", @"img3.png", nil]; for (int i = 0; i < [imagesArray count]; i++) { CGFloat xOrigin = i * scrollView.frame.size.width; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)]; [imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]]; [imageView setContentMode:UIViewContentModeScaleAspectFit]; [scrollView addSubview:imageView]; } [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [imagesArray count], scrollView.frame.size.height)]; 
+5
source

Apple.developer PhotoScroller demonstrates the use of built-in UIScrollViews and CATiledLayer to create a rich user interface for displaying and paging photos that can be individually tinted and enlarged.

CATiledLayer is used to improve the performance of paging, panning and scaling high-resolution images or large sets of photographs.

+2
source

you are just the first to create a new representation of the runtime and the image and assign a tag of all kinds, for example 1000 or 100, and increase the value of the tag, and then add this view to the scroll and two buttons to move left and right and do their actions on the output and add all scrollview routines in to mutable array like this

  indexStart=100; UIView *AddView=[[UIView alloc]initWithFrame:CGRectMake(55, 0, 100, 100)]; AddView.tag=indexStart; btnTap.tag=indexStart; UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; [AddView addSubview:imgview]; imgview.image=image; imgview.tag=1000; [ScrollView addSubview:AddView]; [imgArray addObject:AddView]; 

you use a loop for this, and on the left and right buttons you use this code, and in this code editImgeView is a representation of the image between the left and right buttons, where you display the image, just a simple indexstart ++ or - if the user selects the right or left button

 for (int index = 0; index < [imgAddArrayAfter count]; index ++ ) { NSLog(@"index %d",index); UIView *vc=[imgArray objectAtIndex:index]; NSLog(@"view tag %d",vc.tag); if(vc.tag == indexStart) { UIImageView *modalView=(UIImageView *) [vc viewWithTag:1000]; editImgeView.image=modalView.image; } } 

I hope you understand; -)

0
source
 - (void)viewDidLoad { [super viewDidLoad]; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; scrollView.contentSize = CGSizeMake(320, 465); [scrollView setScrollEnabled:YES]; [scrollView setPagingEnabled:YES]; [scrollView setAlwaysBounceVertical:NO]; [self.view addSubview:scrollView]; NSMutableArray *arrImage = [NSMutableArray arrayWithObjects:@"food1.jpeg", @"food2.jpeg", @"food3.jpeg",@"food4.jpeg", @"food5.jpeg", @"food6.jpeg",@"food7.jpeg", @"food8.jpeg", @"foo9.jpeg",@"food10.jpeg", @"food11.jpeg", @"food12.jpeg",@"food13.jpeg", @"food14.jpeg", @"food15.jpeg", @"food16.jpeg", nil]; for (int i = 0; i < [arrImage count]; i++) { CGFloat xOrigin = i * scrollView.frame.size.width; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)]; [imageView setImage:[UIImage imageNamed:[arrImage objectAtIndex:i]]]; [imageView setContentMode:UIViewContentModeScaleAspectFit]; [scrollView addSubview:imageView]; } [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [arrImage count], scrollView.frame.size.height)]; } 
0
source

All Articles