如何制作UIScrollView图库? (iPhone SDK)

时间:2010-02-18 20:28:15

标签: iphone sdk uiimageview gallery

当滚动UIScrollView时,我一直在努力让我的UIImageView改变它的图像,有点像Photos.app。

这是我的代码:

- (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"] ofType:@"caf"];

    }
}

任何提示?谢谢。 (图像的宽度为296。)

4 个答案:

答案 0 :(得分:2)

有两种方法可以完成您的任务:

  1. 尝试学习与您在iPhone中看到的photo.app完全相同的T hree20 TTPhotoViewController
  2. 如果你想自己做,那么根据我的经验,你必须在UIScrollView中添加多个具有正确位置(x,y,宽度,高度)的UIImageView。请记住使用正确的内容大小初始化滚动视图,以便它可以滚动。例如:image1是(0,0,320,480),image2是(320,0,320,480),image3是(640,0,320,480)等...那么你的滚动视图内容大小将是最后一项的x值+宽度最后一项。

答案 1 :(得分:2)

Antohny,

我没有自己的源代码,但我建议您使用以下链接: http://github.com/andreyvit/ScrollingMadness

我认为它会给你一个想法,所以你可以实现它而不会碰到角落。

答案 2 :(得分:0)

您可以使用以下代码执行此操作:

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 ];

答案 3 :(得分:0)

你试试这个

-(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;
}