我有一個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部分,這就是爲什麼我試圖用上面的代碼解決它。但我不能讓它工作。我錯過了什麼?
你不應該在viewForZoomingInScrollView中返回一個新的實例,你應該從你的scrollView中返回現有的子視圖 – fnc12 2015-04-02 07:17:58