1

UIScrollView在 WWDC2010 Session 104 之后,我刚刚在10000 x 8078 px 图像中创建了一个 CATiledLayer 。

我对框架和zoomScale视图首次显示在其顶层感到困扰

  1. 就像在会话 104 中我定义levelsOfDetail为 4
  2. 在我的 drawRect 我打电话CGFloat lScale = CGContextGetCTM(lContext).a;

奇怪的是,在运行时 lScale 被分配了 0.124907158,而不是预期的 0.125。

以及rect传递给drawRect具有浮点值,例如

  • rect.origin.x = 4099.04443
  • rect.origin.y = 6144
  • rect.size.width = 2049.52222
  • rect.size.height = 2048

CATiledLayer对我来说最烦人的是,即使我initWithFrame使用 0x0 原点创建了 tiledImageView ,框架的原点也显示为 0 x 26.93。

有什么想法可以找到对此负责的计算吗?


编辑: 我找到了奇怪框架位置的来源,即 ScrollView 边界被错误地设置为 450 像素。那应该是 420 像素。UIScrollView 中的 layoutSubview 例程有一个中心视图例程,它无意中调整了 y 坐标。

4

2 回答 2

1

我找到了解决这个问题的方法。

WWDC 2010 会话 104 示例有两种UIScrollView控制视图行为的方法。

首先是configureForImageSize我在之前发布的答案中提到了缩放比例的设置。

由于解释的浮点差异,imageView-bounds 会生成前面突出显示的小数(参见 wx/hx 或 wy/hy)。

为了清理它,我使用了layoutSubviews计算视图框架的位置的覆盖:

- (void) layoutSubviews {
    [super layoutSubviews];

    CGSize lBoundsSize = self.bounds.size;
    CGRect lFrameToCenter = imageView.frame;

    // cure to the decimals problem
    lFrameToCenter.size.width  = roundf(lFrameToCenter.size.width);
    lFrameToCenter.size.height = roundf(lFrameToCenter.size.height);

    if (lFrameToCenter.size.width < lBoundsSize.width)
        lFrameToCenter.origin.x = (lBoundsSize.width - lFrameToCenter.size.width) / 2;
    else
        lFrameToCenter.origin.x = 0;

    if (lFrameToCenter.size.height < lBoundsSize.height)
        lFrameToCenter.origin.y = (lBoundsSize.height - lFrameToCenter.size.height) / 2;
    else
        lFrameToCenter.origin.y = 0;

    imageView.frame = lFrameToCenter;

    if ([imageView isKindOfClass:[tileView class]]) {
        imageView.contentScaleFactor = 1.0;
    }
}

请注意,在对它进行任何其他操作之前,我会舍入帧大小!

于 2011-01-06T06:23:21.893 回答
0

我的错误分析更进一步。

UIScrollView我使用configureForImageSize会话 104 示例中的计算时,mImageSize 为 10000x8078 和 320x450 边界,如下所示:

- (void) configureForImageSize:(CGSize)mImageSize  {
    CGSize lBoundsSize = [self bounds].size;

    // set up our content size and min/max zoomscale
    CGFloat lXScale = lBoundsSize.width / mImageSize.width;    // the scale needed to perfectly fit the image width-wise
    CGFloat lYScale = lBoundsSize.height / mImageSize.height;  // the scale needed to perfectly fit the image height-wise

    // debug values
    float wx = mImageSize.width  * lXScale;
    float wy = mImageSize.width  * lYScale;
    float hx = mImageSize.height * lXScale;
    float hy = mImageSize.height * lYScale;

    ...
}

调试器显示以下值:

* mImageSize = width 10000 height 8078
* bounds size: width 320 height 450
* lXScale = 0.0396137647
* lYScale = 0.0450000018
* wx = 320
* wy = 363.51001
* hx = 396.137634
* hy = 450.000031

所以我收集与边界相关的图像大小会导致浮点计算差异。生成时CATiledLayer,边界始终是浮点值。

如果这是原因,我怎样才能得到一个数学方法,而不必调整图像的大小(丑陋的想法)???

于 2011-01-05T09:04:04.193 回答