2014-10-09 57 views
4

我有一個uiscrollview,我加載了一個圖像數組,以便我可以滾動瀏覽圖片的效果類似於IOS中的照片效果。我使用此代碼:啓用放大啓用分頁啓用和滾動的uiscrollview

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *path = [paths objectAtIndex:0]; 
    NSString *imageFilePath = [path stringByAppendingPathComponent:@"newPics"]; 
    fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imageFilePath]; 
    if(fileExists){ 

     imgs=[[NSMutableArray alloc]init]; 


    [imgs addObject:[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/pic1.png"]]]; 
    [imgs addObject:[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/pic2.png"]]]; 
    [imgs addObject:[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/pic3.png"]]]; 
    [imgs addObject:[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/pic4.png"]]]; 


    CGSize imageSize; 
    imageSize.height = 648; 
    imageSize.width = 927; 
    self.scroll1.frame = CGRectMake(0,0,imageSize.width,imageSize.height); 

    self.scroll1.contentSize = CGSizeMake(imageSize.width * imgs.count, imageSize.height); 
    self.scroll1.minimumZoomScale=1.0; 
    self.scroll1.maximumZoomScale=3.0; 
    self.scroll1.pagingEnabled = YES; 


    self.scroll1.delegate=self; 

    CGFloat xPos = 0.0; 

    for (UIImage *image in imgs) { 
     imageView = [[UIImageView alloc] initWithImage:image]; 
     imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.height); 
     [self.scroll1 addSubview:imageView]; 
     xPos += imageSize.width; 

    } 


    } 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    int positionScrollPrim = scrollView.contentOffset.x; 

    int positionScroll = positionScrollPrim/927 ; 

    imageView = [[UIImageView alloc] initWithImage:[imgs objectAtIndex:positionScroll]]; 
    //UIImage *tmep = [imgs objectAtIndex:1]; 

    return imageView; 
} 

我不能達到正確的結果,相反,我得到了一堆奇怪的行爲,如不正確縮放和沒有當我試圖放大更多滾動。我想我的問題是在viewForZoomingInScrollView部分,這就是爲什麼我試圖用上面的代碼解決它。但我不能讓它工作。我錯過了什麼?

+0

你不應該在viewForZoomingInScrollView中返回一個新的實例,你應該從你的scrollView中返回現有的子視圖 – fnc12 2015-04-02 07:17:58

回答

5

常用的方法是使用兩個滾動視圖:外部滾動瀏覽圖像,每個圖像都包含在另一個負責縮放的滾動視圖中。

+0

我現在就試試看吧謝謝你的回覆 – 2014-10-09 08:49:42

+0

你能提供一個代碼片段來知道如何處理它呢?\ – 2014-10-09 08:54:51

+0

我試過了,但它沒有奏效......要麼我迷路了,要麼不能運作,你能幫忙嗎? – 2014-10-09 09:12:02

相關問題