为什么缩放后我的 CATiledLayer 不会在 UIScrollView 中滚动?

发布于 2024-08-30 14:11:41 字数 1892 浏览 4 评论 0原文

我目前在使用 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

I'm currently having the following problem with CATiledLayer: when the view first loads, the scrolling works perfectly, but then when you zoom once, the view snaps to the anchor point (top left corner) and it can no longer scroll at all. The zooming works in that it will zoom in and out, but it will only zoom to the top left corner.

My code is as follows:

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-09-06 14:11:41

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

#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

Here's my first pass at fixing the obvious bugs:

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文