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

发布于 2024-08-22 15:17:45 字数 671 浏览 5 评论 0原文

我一直在努力尝试让 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。)

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

    }
}

Any hints? Thanks. (The images have a width of 296.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

待"谢繁草 2024-08-29 15:17:46

你试试这个

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

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;
}
忘羡 2024-08-29 15:17:45

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

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

There are two ways that accomplish your task:

  1. Try to learn Three20 TTPhotoViewController that will do exactly the same as the photo.app that you see in your iphone.
  2. 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.
指尖微凉心微凉 2024-08-29 15:17:45

安东尼,

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

我认为它会给你一个想法,这样你就可以不拘一格地实施它。

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.

明天过后 2024-08-29 15:17:45

您可以使用以下代码来完成此操作:

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

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 ];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文