在嵌套的UIScrollViews中缩放 [英] Zooming within Nested UIScrollViews

查看:165
本文介绍了在嵌套的UIScrollViews中缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个熟悉的主题我使用UIScrollview创建一个图形小说。该计划是能够通过顶级uiscrollview在小说中翻页。其中包含一系列嵌套的uiscrollviews,其中嵌入了uimageview。我们的想法是,大型图像可以平移并放大,但​​仍然可以分页。我有大部分工作,但我不能让变焦工作。我已经实现了缩放委托方法,但无济于事,缩放只是平移图像。我怀疑这是由于嵌套滚动视图的嵌入性质,但我无法看到我缺少什么。我看了一下Apple的scrollview套件示例,但无法找到答案。

Its a familiar topic Im creating a graphic novel using UIScrollview. The plan is to be able to page along the novel with a top level uiscrollview. Within this are a series of nested uiscrollviews which have a uimageview embedded within each. The idea is that large images can be panned around and zoomed into but still paged along. I have most of this working but I cant get the zoom working. I have implemented the zoom delegate method but to no avail, zooming just pans the image around. I suspect this is something due to embedded nature of the nested scrollviews but I cant see what im missing. I had a look at the Apple scrollview suite example but couldnt find an answer.

这是我到目前为止所做的工作:

This is what I have working so far:

  - (void)viewDidLoad

{

[super viewDidLoad];


UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
scrollview.pagingEnabled = YES;
scrollview.scrollEnabled =YES;
scrollview.clipsToBounds = NO;
scrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollview.showsHorizontalScrollIndicator = YES;
scrollview.backgroundColor = [UIColor blackColor];

NSInteger viewcount=4;
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"mars.png"],[UIImage imageNamed:@"mercury.png"],[UIImage imageNamed:@"neptune.png"],[UIImage imageNamed:@"venus.png"],nil];

for (int i = 0; i <viewcount; i++)
{
   CGFloat x = i * self.view.frame.size.width;

    subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];


    [subView setBackgroundColor:[UIColor blackColor]];
    [subView setCanCancelContentTouches:NO];
    subView.clipsToBounds = NO;    // default is NO, we want to restrict drawing within our scrollview
    subView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    aImageView = [[UIImageView alloc ] initWithImage:[images objectAtIndex:i]];
    [self.aImageView setTag:ZOOM_VIEW_TAG];
    [subView addSubview:aImageView];
    [subView setContentSize:CGSizeMake(aImageView.frame.size.width, subView.frame.size.height)];
    subView.minimumZoomScale = 1;
    subView.maximumZoomScale = 3;
    subView.delegate = self;
    [subView setScrollEnabled:YES];
     subView.contentSize = aImageView.frame.size;
    [scrollview addSubview:subView];
    [subView release];
}



[self.view addSubview:scrollview];

}



-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
NSLog (@"test");
UIView * view = nil;
view = [subView viewWithTag:ZOOM_VIEW_TAG];

return view;
}


推荐答案

为任何人解决这个问题否则试图这样做。

Worked this out, for anyone else trying to do this.

有一件事我在上面的帖子上错过了一行代码,这是非常基础的!在viewdidLoad方法中,
就在[self.view addSubview:滚动视图]。应该有以下计算所有子视图的宽度

One thing I missed a line of code out on the post above which is quite fundamental!, in the viewdidLoad method just above [self.view addSubview:scrollview]; there should be the following which calculates the width of all of the subviews

 scrollview.contentSize = CGSizeMake(self.view.frame.size.width*viewcount,self.view.frame.size.height);

- (UIView *)viewForZoomingInScrollView中的代码:(UIScrollView *)scrollView {
方法应该是:

The code in the -(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView { method should be:

return [scrollView.subviews objectAtIndex:0];  

这使您可以缩放和平移图像,也可以很好地沿着一组图像进行翻页。刚刚开始研究如何在转移到下一个图像时自动缩放回原始大小,因为它在分页时保持缩放...

This allows you to zoom and pan around images and also page along a set of images very nicely. Just got to work out how to zoom back to the original size automagically when moving on to the next image as its stays zoomed upon paging along..

这篇关于在嵌套的UIScrollViews中缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆