【问题标题】:Why won't my CATiledLayer scroll in a UIScrollView after zooming?为什么我的 CATiledLayer 在缩放后不会在 UIScrollView 中滚动?
【发布时间】:2010-04-29 15:03:54
【问题描述】:

我目前在使用 CATiledLayer 时遇到以下问题:当视图首次加载时,滚动效果很好,但是当您缩放一次时,视图会捕捉到锚点(左上角)并且无法再滚动一点也不。缩放的工作原理是它会放大和缩小,但它只会放大到左上角。

我的代码如下:

#import <QuartzCore/QuartzCore.h>
#import "PracticeViewController.h"

@implementation practiceViewController
//@synthesize image;

- (void)viewDidLoad
{


    NSString *path = [[NSBundle mainBundle] pathForResource:@"H-5" ofType:@"jpg"]; 
    NSData *data = [NSData dataWithContentsOfFile:path];
    image = [UIImage imageWithData:data];

    CGRect pageRect = CGRectMake(0,  0,  image.size.width, image.size.height);  


    CATiledLayer *tiledLayer = [CATiledLayer layer];
    tiledLayer.anchorPoint = CGPointMake(0.0f, 1.0f);
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1000, 1000);
    tiledLayer.levelsOfDetail = 6; 
    tiledLayer.levelsOfDetailBias = 0;
    tiledLayer.bounds = pageRect; 
    tiledLayer.transform = CATransform3DMakeScale(1.0f, -1.0f, 0.3f);

    myContentView = [[UIView alloc] initWithFrame:self.view.bounds];
    [myContentView.layer addSublayer:tiledLayer];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.minimumZoomScale = .2;   
    scrollView.maximumZoomScale = 1; 
    [scrollView addSubview:myContentView];
    [self.view addSubview:scrollView];


}

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

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"H-5" ofType:@"jpg"]; 
    NSData *data = [NSData dataWithContentsOfFile:path];
    image = [UIImage imageWithData:data];

    CGRect imageRect = CGRectMake (0.0, 0.0, image.size.width, image.size.height);
    CGContextDrawImage (ctx, imageRect, [image CGImage]);
}

@end

【问题讨论】:

  • Stack Overflow 不适用于执行代码审查。如果您有具体问题,请提出与这些问题相关的问题。
  • 我确实有一个特定的问题,我在下面的 lucius 的回答中记录了该问题。 “好的,我已经更新了我的代码以反映我现在所拥有的,现在真的只有一个问题。当视图第一次加载时,滚动效果很好。但是当你缩放一次时,视图会捕捉到锚点(顶部左角)并且它根本无法滚动。缩放的工作原理是它会放大和缩小,但它只会放大到左上角。“不知道你在抱怨什么
  • 4598 - 您的问题中没有实际问题,解析 cmets 以找出所问的内容并不是很简单。我已经编辑了您的问题以反映您现在的问题。
  • 4598 - 在当前的编辑中,我还标记了您的新问题:stackoverflow.com/questions/2750320/… 与此问题重复,因为它在问同样的问题。无意冒犯,我只是想让事情井井有条。
  • 好的,谢谢。只是想解决最后一个问题。

标签: iphone ipad catiledlayer


【解决方案1】:

这是我修复明显错误的第一步:

#import <QuartzCore/QuartzCore.h>
#import "PracticeViewController.h"

@implementation PracticeViewController
@synthesize image;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // You do not need to and must not autorelease [NSBundle mainBundle]
    NSString *path = [[NSBundle mainBundle] pathForResource:@"H-5" ofType:@"jpg"]; 
    NSData *data = [NSData dataWithContentsOfFile:path];
    image = [UIImage imageWithData:data];

    CGRect pageRect = CGRectMake(0,  0,  1600, 2400);

    CATiledLayer *tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
    tiledLayer.levelsOfDetail = 5; // was 1000, which makes no sense. Each level of detail is a power of 2.
    tiledLayer.levelsOfDetailBias = 0; // was 1000, which also makes no sense. 
    // Do not change the tiledLayer.frame.
    tiledLayer.bounds = pageRect; // I think you meant bounds instead of frame.
    // Flip the image vertically.
    tiledLayer.transform = CATransform3DMakeScale(zoom, -zoom, 1.0f);

    myContentView = [[UIView alloc] initWithFrame:self.view.bounds];
    [myContentView.layer addSublayer:tiledLayer];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.maximumZoomScale = 32; // = 2 ^ 5
    [scrollView addSubview:myContentView];

    [self.view addSubview:scrollView];
}

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

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"H-5" ofType:@"jpg"]; 
    NSData *data = [NSData dataWithContentsOfFile:path];
    image = [UIImage imageWithData:data];
    CGRect imageRect = CGRectMake (0.0, 0.0, image.size.width, image.size.height);
    CGContextDrawImage (context, imageRect, [image CGImage]);
}

@end

【讨论】:

  • 非常感谢 - 请参阅我上面的编辑。视图现在加载图像,但滚动和缩放不起作用。当我放大或缩小时,它会自动滚动视图,以便某个点(可能是图像的中心,但我不确定)位于左上角。此外,瓷砖加载速度比我希望的要慢(每个瓷砖大约 2 秒)是正常的吗?
  • 另外 - 在我尝试缩放之前滚动将正常工作,一旦我放大或缩小,滚动将根本不起作用
  • 使用 image.size 获取图像大小。瓦片加载非常缓慢,因为它必须按照现在编写代码的方式为每个瓦片解压缩 JPG。您应该将整个 UIImage 存储在内存中并进行绘制,或者将其分割成图块,这就是我在我的 Transit Maps iPhone 应用程序中所做的。缩放基于 tiledLayer.anchorPoint,每个轴的范围从 0.0 到 1.0。您需要将锚点设置为 {0.5,0.5} 之类的东西,以便它从中心缩放。您还需要将 tileLayer.position 设置为屏幕中心。
  • 好的,我已经更新了我的代码以反映我现在所拥有的,现在真的只有一个问题。当视图首次加载时,滚动效果很好。但是,当您缩放一次时,视图会捕捉到锚点(左上角),并且根本无法滚动。缩放的工作原理是它会放大和缩小,但它只会放大到左上角。
  • 很难理解为列出的问题提供的解决方案。我只对问题的某些方面感兴趣,但我不能轻易地从这里提取选择性信息。我想知道您如何解决缩放后无法滚动的问题
猜你喜欢
  • 2016-07-05
  • 2011-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 2011-11-10
  • 1970-01-01
  • 2011-11-15
相关资源
最近更新 更多