简体   繁体   English

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

[英]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. 我一直在努力尝试让我的UIImageView在滚动UIScrollView时更改其图像,有点像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.) (图像的宽度为296。)

There are two ways that accomplish your task: 有两种方法可以完成您的任务:

  1. Try to learn T hree20 TTPhotoViewController that will do exactly the same as the photo.app that you see in your iphone. 尝试学习与您在iPhone中看到的photo.app完全相同的T hree20 TTPhotoViewController
  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. 如果您想自己做,那么根据我的经验,您必须将多个具有正确位置(x,y,宽度,高度)的UIImageView添加到UIScrollView中。 Remember to initialize the scroll view with the right content-size so that it's scrollable. 请记住使用正确的content-size初始化滚动视图,以便它可以滚动。 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. 例如:image1是(0,0,320,480),image2是(320,0,320,480),image3是(640,0,320,480),依此类推...那么您的滚动视图内容大小将是最后一项的x值+宽度最后一项。

Antohny, 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 我没有自己的源代码,但是我可以建议您使用以下链接: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM