2

我一直在努力尝试让我的 UIImageView 在 UIScrollView 滚动时更改其图像,有点像 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

4 回答 4

2

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

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

安东尼,

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

我认为它会给你一个想法,这样你就可以在不走弯路的情况下实施它。

于 2010-02-18T21:19:23.743 回答
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 ];
于 2011-04-06T13:27:57.753 回答
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;
}
于 2011-08-24T10:04:26.980 回答