2010-04-29 101 views
1

我目前对CATiledLayer有以下问题:当视图第一次加载时,滚动完美地工作,但是当你缩放一次时,视图捕捉到锚点(左上角),并且它不能滚动到所有。缩放的作用在于可以放大和缩小,但只能缩放到左上角。为什么我的CATiledLayer在缩放后不能在UIScrollView中滚动?

我的代码如下:

#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 
+1

堆栈溢出并不打算用于进行代码审查。如果您遇到特定问题,请提出相关问题。 – 2010-05-03 22:57:46

+0

我确实有一个具体的问题,我已经在Lucius的答案中记录下。 “好吧,我已经更新了我的代码以反映我现在的情况,现在真的只有一个问题,当视图第一次加载时,滚动效果非常好,但是当您缩放一次时,视图会捕捉到锚点(顶部它不能再滚动,缩放的作用在于它可以放大和缩小,但它只能放大到左上角。“不知道你在抱怨什么 – Brodie 2010-05-04 01:33:29

+0

4598 - 没有真正的问题在你的问题中,解析这些评论以找出问题并不是非常简单。我编辑了你的问题,以反映你现在问的问题。 – 2010-05-04 03:33:27

回答

0

这是我在固定明显错误第一遍:

#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 
+0

非常感谢 - 请参阅我上面的修改。视图现在加载图像,但滚动和缩放不起作用。当我放大或缩小时,它会自动滚动视图,以便某个点(可能是图像的中心但不确定)位于左上角。此外瓷砖的加载速度比我所希望的要慢(每个瓷砖大约2秒)是否正常? – Brodie 2010-04-29 21:41:46

+0

也 - 滚动将正常工作,直到我尝试缩放,并且只要我放大或缩小,滚动将根本不起作用 – Brodie 2010-04-29 21:49:04

+0

使用image.size获取图像大小。由于需要现在编写代码的方式来为每个图块解压缩JPG,所以图块加载非常缓慢。您应该将整个UIImage存储在内存中并绘制它,或者将其分割为多个tile,这正是我在Transit Maps iPhone应用程序中所做的。缩放基于tiledLayer.anchorPoint,每个轴的范围从0.0到1.0。您需要将定位点设置为{0.5,0.5},才能从中心放大。您还需要将tileLayer.position设置为屏幕中心。 – lucius 2010-04-30 06:44:30

相关问题