2010-02-18 63 views
2

我一直在努力尝试让UIImageView在UIScrollView滚动时更改图像,有点像Photos.app。如何制作UIScrollView图片库? (iPhone SDK)

这里是我的代码:

- (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的宽度)

回答

2

有两种方式完成任务:

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

要了解更多关于TTPhotoViewController退房:https://github.com/facebook/three20 – 2011-02-04 18:16:36

2

Antohny,

我没有我自己的代码就在我身边,但我可以建议你将以下链接: http://github.com/andreyvit/ScrollingMadness

我认为它会给你一个想法,这样你就可以实施它而不会碰到角落。

+0

好东西,但是如何在我需要时在我的ViewController上放置ATPagingView View?我仍然不明白... – 2012-01-18 16:28:16

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