I am trying to display images in a horizontal row using pages,with each page containing 4 images probably separated by a little space of one line or without space. As the user scrolls horizontally, next four images are displayed in the center of view. But using this code:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, screenHeight/4, screenWidth, screenHeight/4)];
scroll.showsHorizontalScrollIndicator = YES;
scroll.showsVerticalScrollIndicator=NO;
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
int k=1;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
for (int j=0; j<4; j++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.png", k];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect imageFrame=CGRectMake(0, k*screenHeight/4, screenWidth/5, screenHeight/5);
imageView.frame=imageFrame;
[awesomeView addSubview:imageView];
k++;
}
images are displayed vertically on first page view. Moreover it looks like vertical scrolling is working. What I am missing?
In this line:
CGRect imageFrame=CGRectMake(0, k*screenHeight/4, screenWidth/5, screenHeight/5);
You are messing with frame your imageView (inside UIView - awesomeView).
Your k variable is 1,2,3,4
So i assume that you have 4 imageViews in vertical, and next 'column' with four image on the left, right?
Also you don't set contentSize for your scrollView anywhere so, you might not see the second 'column'
Related
I have an UIImageView and I want to implement fling feature, when users flings on the screen the page can go up or down quickly.
Now my thinks is add two UISwipeGesturesRecognizers, but I don't know how to do next, should I use an animation to do this, and how to calculate the animation distance and time?
Also I find other answers said no need gesture recognizer can use scroll view, I am totally confused, are there any sample I can learn?
You can do it like that :
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setPagingEnabled:YES];
[scrollView setAlwaysBounceVertical:YES];
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, scrollView.frame.size.height*[imagesArray count])];
I have a UIScrollView, inside that are UIImageViews all 198*198pts. The images in this scrollview all seem to be slightly distorted. The edges are more jagged (I am working with png files and the images are cartoons/stickers). In a normal UIImageView not contained in a UIScrollView they look fine. Could anyone give me some pointers to why this might be happening?
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width*[self.stickers count], self.scrollView.frame.size.height)];
for(int i=0; i<[self.stickers count]; i++){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
MySticker *sticker = self.stickers[i];
imageView.image=sticker.image;
imageView.contentMode=UIViewContentModeScaleAspectFill;
[content addSubview:imageView];
//NSLog(#"scroll view width is %f",self.scrollView.frame.size.width);
//NSLog(#"ImageView width is %f", imageView.frame.size.width);
}
self.scrollView.contentSize=CGSizeMake(content.frame.size.width ,content.frame.size.height);
[self.scrollView addSubview:content];
EDIT ^ added my scrollview setup above
I did Get images for Url.and then display on SCrollView.My code like this
NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
for (NSDictionary *diction in al)
{
NSString *geting =[diction valueForKey:#"dealimage"];
NSLog(#"dealimage is %#",geting);
NSData *getdata=[[NSData alloc]initWithData:[NSData dataFromBase64String:geting]];
NSLog(#"good day %#",getdata);
dataimages=[UIImage imageWithData:getdata];
NSLog(#"dataimages %#",dataimages);
[imagesarray addObject:dataimages];
}
scrollView=[[UIScrollView alloc]init];
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 100;
scrollView.contentSize = CGSizeMake(scrollWidth,100);
scrollView.frame=CGRectMake(0, 0, 320, 100);
scrollView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:scrollView];
int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
for(int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(5+xOffset, 0, 160, 110);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
[images insertObject:img atIndex:index];
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[scrollView addSubview:[images objectAtIndex:index]];
xOffset += 170;
}
my Problem is ScrollView is Display but Images are not add So please tell me what wrong in my code
now i need like this
I know how to get images for Url.now i need display images Horizontal ScrollView So Please give me any idea about images .
your coding is Fine, but you are assign the value of [scrollView addSubview:[images objectAtIndex:index]];, this is not the fine one, I modify your code, as per your result Want
UIScrollView *scrollVie=[[UIScrollView alloc]init];
scrollVie.delegate = self;
scrollVie.scrollEnabled = YES;
int scrollWidth = 100;
//scrollVie.contentSize = CGSizeMake(scrollWidth,100);
scrollVie.frame=CGRectMake(0, 51, self.view.frame.size.width, 100);
scrollVie.backgroundColor=[UIColor redColor];
int xOffset = 0;
for(int index=0; index < [imagesarray count]; index++)
{
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(xOffset,10,160, 110)];
img.image = (UIImage*) [imagesarray objectAtIndex:index];
[scrollVie addSubview:img];
xOffset+=100;
}
[self.view addSubview:scrollVie];
scrollVie.contentSize = CGSizeMake(scrollWidth+xOffset,110);
"official" example created by Apple take a look at the StreetScroller Demo.
For the desired outcome that is shown in the picture, ie, the horizontal scroll,
Use ScrollView instead of tableview.
set the pane of scroll to horizontal and dynamically create imageview inside a loop and insert images inside it
hope it helps :)
Instead of using table view why don't you user UICollectionView with their delegates.
you can refer this - Creating a UICollectionView programmatically
It's best way to achieve your requirement.
I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}
I have a UIScrollView that I would like to use with a UIPageControl to have different pages to swipe horizontally. I need to add a UITextView and a number of UIImageView for every page.
In my ViewController I call a webservice and, when data arrive, I add items as follows:
for(int i=0;i<[arrData count];i++) {
NSString *body=#"Dummy text";
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size.width = self.scrollView.frame.size.width;
frame.size.height = 40; //vedere landscape
UILabel *label = [[UILabel alloc] initWithFrame:frame];
[label setText:body];
label.numberOfLines=0;
[label sizeToFit];
label.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.scrollView addSubview:label];
[label setBackgroundColor:[UIColor whiteColor]];
[self.scrollView addSubview:label];
int y_pos=label.frame.size.height+10;
int x_pos=0;
for(int img=0; img<[[[arrData objectAtIndex:i]objectForKey:#"images"] count]; img++) {
NSString *imageURL=[[[[arrData objectAtIndex:i] objectForKey:#"images"] objectAtIndex:img] objectForKey:#"fn"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(self.scrollView.frame.size.width * i+img*100, y_pos, image.size.width, image.size.height)];
[self.scrollView addSubview:imageView];
}
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [arrData count], self.scrollView.frame.size.height);
It runs fine in portrait, but when the device is rotated, obviously, the layout is ruined ... which is the best way to work with a ScrollView with PageControl? Do I have to handle rotation and change the distance between the inserted items or (as I think) is there a better way to build everything?
yes you have to set your scrollviews frame again every time orientation changes, because flexible width stretches the width of scrollview from both sides in landscape mode,so its contents get overlapped and causes problem when you have horizontal scrolling. one thing you can do is set you scrollview's auto resizing mask to flexibleRightMargin|flexibleLeftMargin. it will keep the contents of your scrollview at the center but scrolling will not look clean.