为什么我的CATiledLayer在缩放后不会在UIScrollView中滚动? [英] Why won't my CATiledLayer scroll in a UIScrollView after zooming?

查看:118
本文介绍了为什么我的CATiledLayer在缩放后不会在UIScrollView中滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的代码如下:

#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


推荐答案

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

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

这篇关于为什么我的CATiledLayer在缩放后不会在UIScrollView中滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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