ScrollView for images Objective C (iOS) - ios

I am trying to create a welcome page for my app (with screenshots).
I have taken some samples and made it into a text scroll. I have tried for weeks without any success. Thanks
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.textAlignment = UITextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
Here is my failed attempt to do with images:
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UIImageView *tmpLabel = [[UIImageView alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.imageAlignment = UIImageAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:subview1.frame];
if (i == 0) {
imageView1.image = [UIImage imageNamed:#"openingScreen1.png"];
}else if (i == 1){
imageView1.image = [UIImage imageNamed:#"openingScreen2.png"];
}else if (i == 2){
imageView1.image = [UIImage imageNamed:#"openingScreen3.png"];
}else {
imageView1.image = [UIImage imageNamed:#"openingScreen4.png"];
}
I also might need to remove the word "Parse" unless the images above sit on top of it? I cannot locate the word.

try this
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
[someScrollView setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfPages = 5;
for (int i = 0; i < numberOfPages; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width+10;
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin,70,self.view.frame.size.width,25)];
tmpLabel.textAlignment = NSTextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i+1];
[someScrollView addSubview:tmpLabel];
// calculate the width
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, tmpLabel.frame.size.height + tmpLabel.frame.origin.y + 10,
self.view.frame.size.width,
self.view.frame.size.height)]; // customize your width, height and y co ordinates
image.image = [UIImage imageNamed:[NSString stringWithFormat:
#"openingScreen%d.jpg", i+1]];
image.contentMode = UIViewContentModeScaleAspectFit;
[someScrollView addSubview:image];
}
//set the scroll view content size
someScrollView.backgroundColor = [UIColor blueColor];
someScrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfPages,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:someScrollView];
the sample OutPut is
here I attached the sample Project also, please use this

Please change your code to something like
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
//set the scroll view delegate to self so that we can listen for changes
someScrollView.delegate = self;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 1; i <= numberOfPages; i++) {
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//get the sub view and set the frame
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, someScrollView.frame.size.height)];
imageView1.image = [UIImage imageNamed: [NSString stringWithFormat:#"openingScreen%d.png",i ]];
//add the subview to the scroll view
[someScrollView addSubview:imageView1];
}
Add scroll view Delegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
//dragging ends, please switch off paging to listen for this event
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *) targetContentOffset NS_AVAILABLE_IOS(5_0)
{
//find the page number you are on
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(#"Dragging - You are now on page %i",page);
}

Related

How to Set the View Using for Loop?

I have to design screen like this.The Essential Category Contains 4 items,then Kitchen category Contains 8 items,then utility category contains 11 items.How to do this with reusability?
This is My Code which i tried:
- (void)buildAppliancesView
{
for (int i=0; i<5; i++)
{
[self addAppliancesCategoriesLabel:i];
[self addApplianceCategoryView:i];
[self addIndividualAppliances:i];
}
}
- (void)addAppliancesCategoriesLabel:(int)y
{
label = [[UILabel alloc] initWithFrame:CGRectMake(0,(_appliancesHeaderLabel.frame.origin.y+_appliancesHeaderLabel.frame.size.height)+y*150+10, 0, 0)];
NSLog(#"y=>%f",label.frame.origin.y);
label.text =[NSString stringWithFormat:#"%#",[APPLIANCESCATEGORIESLABEL_ARRAY objectAtIndex:y]];
[label sizeToFit];
[_scrollView addSubview:label];
}
- (void)addApplianceCategoryView:(int)y
{
applianceCategoryView = [[UIView alloc]initWithFrame:CGRectMake(5, label.frame.origin.y+label.frame.size.height+5, self.view.frame.size.width-10, 70)];
[applianceCategoryView setBackgroundColor:[UIColor grayColor]];
[_scrollView addSubview:applianceCategoryView];
}
- (void)addIndividualAppliances:(int)y
{
for (int i=0; i<4; i++)
{
UIView *applianceView = [[UIView alloc]initWithFrame:CGRectMake(5+((self.view.frame.size.width/4)+5)*y, _appliancesHeaderLabel.frame.origin.y+_appliancesHeaderLabel.frame.size.height+label.frame.size.height+10, 50, 50)];
NSLog(#"xVAlue==>>%f",applianceView.frame.origin.x);
[applianceView setBackgroundColor:[UIColor redColor]];
UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(changecolor)];
[applianceView addGestureRecognizer:singleTap];
[applianceCategoryView addSubview:applianceView];
}
}
Check this - sample code for creating views in loop.
int xPosition = 0;
int yPosition = 0;//set your y origin
int height = 150; // sample value
int width = (NumberOfItems.count)/4;
for (int i = 0; i < NumberOfItems.count; i++) {
UIView * view = [[UIView alloc] initFrame:CGRectMake(xPosition, yPosition, width, height)];
[self.scrollView addSubview:view];
xPosition += width;
if(xPosition == 3*width){
xPosition=0;
yPosition = yPosition + height; // set as per your requirement
}
}
Using UIScrollview for this will be better.
OR
Here is the sample code for your scenario.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapDetected:)];
// singleTap.numberOfTapsRequired=1;
UIScrollView *ScrollVc = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
ScrollVc.delegate = self;
ScrollVc.showsHorizontalScrollIndicator = NO;
[ScrollVc setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:ScrollVc];
ScrollVc.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// [ScrollVc addGestureRecognizer:singleTap];
NSArray *essentialsArr = #[#"1",#"2",#"3"];
NSArray *KitchenArr = #[#"1",#"2",#"3",#"4",#"5",#"6"];
NSArray *abcArr = #[#"1",#"2",#"3",#"4"];
NSArray *arr = [NSArray arrayWithObjects:essentialsArr,KitchenArr,abcArr, nil];
int yPosition = 0;//set your y origin
int yPositionOfMain = 0;//set your y origin
for (int k = 0; k < arr.count; k++) {
int xPosition = 0;
int height = 100; // sample value
int width = ([[UIScreen mainScreen] bounds].size.width)/4;
UIView *mainview = [[UIView alloc] init];
mainview.backgroundColor = [UIColor yellowColor];
[ScrollVc addSubview:mainview];
for (int i = 0; i < [[arr objectAtIndex:k] count]; i++) {
if (i==0) {
xPosition=0;
yPosition= 0;
}
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(xPosition + 10, yPosition, width-20, height)];
view.backgroundColor = [UIColor redColor];
[mainview addSubview:view];
xPosition += width;
if(xPosition == 4*width && i!=[[arr objectAtIndex:k] count]-1){
xPosition=0;
yPosition = yPosition + height + 10; // set as per your requirement
}
}
mainview.frame = CGRectMake(0, yPositionOfMain, [[UIScreen mainScreen] bounds].size.width, yPosition + height + 10);
yPositionOfMain = yPositionOfMain + mainview.frame.size.height + 20;
}
if ([UIScreen mainScreen].bounds.size.height==736)// iPhone 6 Plus 66 Navigation bar
{
[ScrollVc setContentSize:CGSizeMake(320,950)];
}
else if ([UIScreen mainScreen].bounds.size.height==667)// iphone 6 44 Navigation bar
{
[ScrollVc setContentSize:CGSizeMake(320,900)];
}
else if ([UIScreen mainScreen].bounds.size.height==568)// iphone 5, 5C, 5S 44 Navigation bar
{
[ScrollVc setContentSize:CGSizeMake(320,800)];
}
else // iphone 4, 4S 44 Navigation ba
{
[ScrollVc setContentSize:CGSizeMake(320,750)];
}
}
Please change the necessary values as per your requirements.
Hope this helps.

scrollView with paging enabled not working properly ? (Objective-C)

i want to create paging scrollView with three UIViews. And than wants to add imageView as a subView to these UIViews. i cant figure it out how to calculate the frame of imageViews.
When i run the code without imageViews it works perfectly fine.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.frame = self.view.frame;
NSArray *colorsArray = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor yellowColor],[UIColor greenColor], nil];
NSArray *imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"123.png"],[UIImage imageNamed:#"13.png"],[UIImage imageNamed:#"12.png"],nil];
for (int i = 0; i < colorsArray.count; i++) {
CGRect frame;
frame.origin.x = self.view.frame.size.width * i;
frame.size = self.view.frame.size;
self.scrollView.pagingEnabled = YES;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [colorsArray objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [imagesArray objectAtIndex:i];
[view addSubview:imageView];
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * colorsArray.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.scrollView.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}
The frame for the image view needs to be relative to its superview (i.e. view) not the scroll view. To achieve the paging you desire change:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
to:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height)];

How to start the photo scroller with the selected image from collectionView in ios

I have used the code
imageSlider = [[UIScrollView alloc]initWithFrame:CGRectMake(5, 60, 700, 700)];
imageSlider.pagingEnabled = YES;
imageSlider.showsHorizontalScrollIndicator =NO;
for (int i = 0; i < [myObject count]; i++) {
CGRect frame;
frame.origin.x = imageSlider.frame.size.width * i;
frame.origin.y = 0;
frame.size = imageSlider.frame.size;
NSManagedObject *con = [myObject objectAtIndex:i] ;
UIImage *Image = [UIImage imageWithData:[con valueForKey:#"image"]];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 700, 700)];
imgView.image = Image;
imgView.frame = frame;
[imageSlider addSubview:imgView];
}
imageSlider.contentSize = CGSizeMake(imageSlider.frame.size.width*([myObject count]), 600);
imageSlider.delegate = self;
[self.view addSubview:imageSlider];
It starts from first image I dono how to start image from the selected image in previous viewController. Thanks in advance..

How to add images in nested UIScrollview

I need to add images in scroll view multiple times. Am created a scrollview but images not append correctly in that.
My code is here :
-(void)sampleScroll
{
int x = 10;
int y = 20;
mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);
// further configure
[self.view addSubview: mainScrollView];
images = [[NSMutableArray alloc] initWithObjects:#"image0.jpg",#"image1.jpg",#"image2.jpg",#"image3.jpg", nil];
for(int i=0; i<[images count]; i++)
{
NSLog(#"%#",images);
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 250, 150)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor whiteColor];
NSString *img = [images objectAtIndex:i];
NSLog(#"%#",img);
imageView.image = [UIImage imageNamed:#"image3.jpg"];
NSLog(#"%#",imageView.image);
scrollview.contentSize = CGSizeMake(1250,250);
[scrollview addSubview:imageView];
[mainScrollView addSubview:scrollview];
y=y+155;
//[self myscrollView];
}
}
Please give me a solution. Thanks in advance..
Frame adjustment needed,
int x = 10;
int y = 10;
UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(mainScrollView.frame.size.width, mainScrollView.frame.size.height * COUNTS);
mainScrollView.pagingEnabled = YES;
for(int i = 0; i < COUNTS; i++)
{
UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, mainScrollView.frame.size.width - 20.0, mainScrollView.frame.size.height - 20.0)];
scrollview.showsVerticalScrollIndicator = YES;
scrollview.scrollEnabled = YES;
scrollview.userInteractionEnabled = YES;
scrollview.backgroundColor = [UIColor whiteColor];
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x,10.0, mainScrollView.frame.size.width - 40.0, mainScrollView.frame.size.height - 40.0)];
imageView.image = [UIImage imageNamed:#"owl.jpg"];
[scrollview addSubview:imageView];
[mainScrollView addSubview:scrollview];
y += mainScrollView.frame.size.height;
}
[self.view addSubview:mainScrollView];

Scrollview not creating through programmatically

I want to creat a scrollview programmatically but the scroll view is not creating the below is my code i want to display images in that scrollview.
fscroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view1.frame.size.width, self.view1.frame.size.height)];
fscroll.contentSize = CGSizeMake(320, 400);
fscroll.backgroundColor = [UIColor blackColor];
fscroll.showsHorizontalScrollIndicator = YES;
[view1 addSubview:fscroll];
int X=0;
for (int i = 0; i < [images count]; i++)
{
imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 320, 480)];
imageView.backgroundColor = [UIColor blackColor];
[imageView setImage: [images objectAtIndex:[sender tag]]];
[imageView addSubview:fscroll];
X = X + imageView.frame.size.height;
if(X > 320)
self.fscroll.contentSize = CGSizeMake(X, 134);
if(X > 320)
self.fscroll.contentSize = CGSizeMake(X, 134);
}
X = X + imageView.frame.size.height;
Did you mean to do this?
X = X + imageView.frame.size.width;
EDIT: Also, are you sure view1 has the right frame set before your UIScrollView is initialized?
This is wrong: [imageView addSubview:fscroll]; Presumable you want to do [fscroll addSubview:imageView];
Simple Method: You can created multiple times if you need means
- (void)scrollView
{
int x = 0; //scrollview width
int y = 10;//scrollview height
for(int i=0; i<5; i++)
{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(50,50);
x=x+55;
}
}

Resources