缩放后我的CATiledLayer滚动在UIScrollView

缩放后我的CATiledLayer滚动在UIScrollView

本文介绍了为什么缩放后我的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中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:40