我有一个UIScrollView,我用一组图片加载它,以便我可以滚动浏览图片,并产生类似于IOS中的照片效果的效果。 我正在使用以下代码:
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部分,所以我尝试使用上面的代码来解决它。但是我就是无法让它工作。我错过了什么?