I’m building a scrollview with images 65×78 size. here is the code:
numberOfViews = [fotosJugadores count];
for (int i = 0; i < [fotosJugadores count]; i++) {
UIImage *myImage = [UIImage imageNamed:[fotosJugadores objectAtIndex:i]];
CGFloat yOrigin = i * myImage.size.width + 120;
NSLog(@"my image %@", myImage);
UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, myImage.size.width, myImage.size.height)];
awesomeView.tag = i;
awesomeView.image = myImage;
awesomeView.alpha = 0.5f;
awesomeView.backgroundColor = [UIColor blackColor];
[self.jugadorSlide addSubview:awesomeView];
awesomeView=nil;
}
[jugadorSlide setBackgroundColor:[UIColor blackColor]];
jugadorSlide.contentSize = CGSizeMake(65 * numberOfViews+240,78);
jugadorSlide.layer.cornerRadius = 11;
jugadorSlide.layer.masksToBounds = YES;
[jugadorSlide setContentOffset:CGPointMake(((65 * numberOfViews)/2), 0)];
//jugadorSlide.decelerationRate = UIScrollViewDecelerationRateNormal;
[self scrollViewDidEndDragging:jugadorSlide willDecelerate:NO];
I’m trying to add a gap of 3 points more or less between images. I add space to the content size and the images x origin in order to leave space between last and first image and scroll view edges, so they always finish in center of scroll view. I have tried different approaches, but I get images overlapped.
Thanks
You are not adding any gap between the images. The yOrigin value is always next to the previous image.
You should add your margin (in your case 3 points) times the image index to the origin, like this:
CGFloat yOrigin = i * (myImage.size.width + 3) + 120;