I have seen this question asked in other places...but no valid answers...
I have a UIScrollView that scrolls horizontally. I have 3 photos in them...ie: subviews of the U开发者_高级运维IScrollView hold UIImageViews as below: (in this case, all the subviews of the UIScrollView are showing path_to_file.png
... for ease of asking this question)
NSString *filepath = @"path_to_file.png";
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
scroll.delegate = self;
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
int maxImageWidth = 0;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat yOrigin = i * self.view.frame.size.width;
UIImage *imgWord = [[UIImage alloc] initWithContentsOfFile:filePath];
UIImageView *imgWordView = [[UIImageView alloc] initWithImage:imgWord];
[imgWord release];
imgWordView.center = CGPointMake ((cell.contentView.frame.size.width/2) + yOrigin,imgWord.size.height/2);
if (cell.contentView.frame.size.width > maxImageWidth) {
maxImageWidth = cell.contentView.frame.size.width;
}
[scroll addSubview:imgWordView];
[imgWordView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[cell.contentView addSubview:scroll];
[scroll release];
Having the above code snippet, I run across the fact that I need:
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
return image;
}
to zoom my UIImageView in the UIScrollView. How do i find out which UIImageView is showing in the UIScrollview so i can in turn access that UIImageView to get the UIImage in order to return it in viewForZoomingInScrollView
?
Please explain this as I do not see how we are to track the 'displayed' subview ourselves...
Thanks!
How do i find out which UIImageView is showing in the UIScrollview so i can in turn access that UIImageView to get the UIImage in order to return it in viewForZoomingInScrollView?
You can track where you are in:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
CGFloat pageWidth = ScrollView.frame.size.width;
page = floor((self.ScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
That lets me know what "page" of content I'm on as the user scrolls through it.
You could subclass the UIImageView class and implement the touch methods.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page_ = (int)round(scrollView.contentOffset.x /scrollView.frame.size.width);
}
精彩评论