如何从uiscrollviews获取“前端”uiimageview?

时间:2011-05-23 21:51:54

标签: iphone uiscrollview uiimage zoom subview

我在其他地方看过这个问题......但没有有效的答案......

我有一个水平滚动的UIScrollView。我有3张照片...即:UIScrollView的子视图保存UIImageViews如下所示:(在这种情况下,UIScrollView的所有子视图都显示path_to_file.png ...以便于询问此问题)

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

有了上面的代码片段,我遇到了我需要的事实:

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return image;
}

在UIScrollView中缩放我的UIImageView。如何在UIScrollview中找出哪个UIImageView 显示,以便我可以依次访问UIImageView获取UIImage以便在viewForZoomingInScrollView中返回它?

请解释一下,因为我不知道自己如何跟踪“显示的”子视图......

谢谢!

3 个答案:

答案 0 :(得分:1)

  

如何找出UIScrollview中显示的UIImageView,以便我可以依次访问UIImageView以获取UIImage以便在viewForZoomingInScrollView中返回它?

您可以追踪您所在的位置:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView


CGFloat pageWidth = ScrollView.frame.size.width;
page = floor((self.ScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; 

这让我知道当用户滚动它时,我所在的内容的“页面”。

答案 1 :(得分:0)

您可以继承UIImageView类并实现触摸方法。

答案 2 :(得分:0)

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page_ = (int)round(scrollView.contentOffset.x /scrollView.frame.size.width);
}