How can I make a UIScrollView image gallery? (iPhone SDK)
I've been struggling with trying to get my UIImageView to change its image when the UIScrollView is scrolled, kind of like the Photos.app.
Here's my code:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x > 296){
self.currentDBNum++;
self.currentDoorbell = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[[self.doorbells objectAtIndex:currentDBNum] objectForKey:@"Image"] ofType:@"jpg"]];
self.doorbellPic.image = currentDoorbell;
self.currentSoundPath = [[NSBundle mainBundle] pathForResource:[[doorbells objectAtIndex:currentDBNum] objectForKey:@"Sound"]开发者_如何学JAVA ofType:@"caf"];
}
}
Any hints? Thanks. (The images have a width of 296.)
There are two ways that accomplish your task:
- Try to learn Three20 TTPhotoViewController that will do exactly the same as the photo.app that you see in your iphone.
- If you want to do it yourself then from my experience, you have to add multiple UIImageView with the correct position (x,y,width,height) to the UIScrollView. Remember to initialize the scroll view with the right content-size so that it's scrollable. An example: image1 is (0,0,320,480), image2 is (320,0,320,480), image3 is (640,0,320,480), etc... then your scroll view content size will be the x's value of the last item + the width of the last item.
Antohny,
I do not have my own source code right with me, but I can suggest you the following link: http://github.com/andreyvit/ScrollingMadness
I think it will give you an idea so you can implement it without hitting the corners.
you can do it by using following code:
UIImageView *tempImageView ;
int initialXPos = 0;
int initialYPos = 30;
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
int arraycounter=0;
for (int i=0; i<numrows; i++)
{
for (int j=0; j<numImagesPerColumn; j++)
{
NSURL *url=[NSURL URLWithString:[aPhoto.childphotos objectAtIndex:arraycounter]];
UIImage *image1;
CGRect image1Frame;
image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
image1Frame = CGRectMake(initialXPos,initialYPos, 80, 80);
tempImageView = [[UIImageView alloc] initWithFrame:image1Frame];
tempImageView.image=image1;
tempImageView.tag = 0;
initialXPos = initialXPos + 80 + 4;
[view addSubview:tempImageView];
arraycounter=arraycounter+1;
}
initialXPos=0;
initialYPos = initialYPos + 86;
}
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:view];
[scrollView addSubview:title];
[scrollView addSubview:des];
[self.view addSubview:scrollView ];
u try this
-(void)getButtonRowSize:(int)_rowsize Count:(int)_count
{
[scrollView setFrame:CGRectMake(0, 0, 320, 480)];
[scrollView setContentSize:CGSizeMake(320,((320-_rowsize*IMAGEGAP)/_rowsize)*(_count/_rowsize +1)+(_count/_rowsize)*IMAGEGAP)];
for(int i=0;i<_count;i++)
{
[scrollView addSubview:[self getButtonRowSize:_rowsize Count:_count currentPos:i]];
}
}
-(UIButton *)getButtonRowSize:(int)_rowsize Count:(int)_count currentPos:(int)_pos
{
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake((_pos%_rowsize)*((320-(_rowsize+1)*IMAGEGAP)/_rowsize)+(_pos%_rowsize +1)*IMAGEGAP,(_pos/_rowsize)*((320-(_rowsize+1)*IMAGEGAP)/_rowsize)+(_pos/_rowsize +1)*IMAGEGAP,((320-(_rowsize+1)*IMAGEGAP)/_rowsize),((320-(_rowsize+1)*IMAGEGAP)/_rowsize))];
[button setBackgroundImage:[self resizingImagewithimagename:[UIImage imageNamed:@"add image file name"] Length:((320-(_rowsize+1)*IMAGEGAP)/_rowsize)] forState:UIControlStateNormal];
return button;
}
精彩评论