【问题标题】:UIScrollView setZoomScale sets the applied rotation back to ZeroUIScrollView setZoomScale 将应用的旋转设置回零
【发布时间】:2023-04-04 00:00:01
【问题描述】:

我从事地图替换工作已经有一段时间了。整个过程与 UIScrollView 一起工作,并由 CATiledLayer 支持。

要旋转我的地图,我会旋转图层本身。 (使用CATransform3DMakeRotation)到目前为止效果很好=)

但是,如果我调用 setZoomScale 方法,将提交到我的图层的 CATransform3D 会将旋转重置为 0。

我的问题是,有什么方法可以设置我的滚动视图的 zoomscale 而不会丢失应用的旋转?

捏合手势也存在同样的问题。

//附加信息

要围绕当前位置旋转,我必须编辑锚点。也许这也是缩放的问题。

- (void)correctLayerPosition {
    CGPoint position    = rootView.layer.position;
    CGPoint anchorPoint = rootView.layer.anchorPoint;
    CGRect  bounds      = rootView.bounds;
    // 0.5, 0.5 is the default anchorPoint; calculate the difference
    // and multiply by the bounds of the view
    position.x              = (0.5 * bounds.size.width) + (anchorPoint.x - 0.5) *    bounds.size.width;
    position.y              = (0.5 * bounds.size.height) + (anchorPoint.y - 0.5) * bounds.size.height;
    rootView.layer.position = position;
}

- (void)onFinishedUpdateLocation:(CLLocation *)newLocation {
    if (stayOnCurrentLocation) {
        [self scrollToCurrentPosition];
    }
    if (rotationEnabled) {
        CGPoint anchorPoint        = [currentConfig layerPointForLocation:newLocation];
        anchorPoint.x              = anchorPoint.x / rootView.bounds.size.width;
        anchorPoint.y              = anchorPoint.y / rootView.bounds.size.height;
        rootView.layer.anchorPoint = anchorPoint;
        [self correctLayerPosition];
    }
}

【问题讨论】:

    标签: ios uiscrollview rotation calayer zooming


    【解决方案1】:

    您可以实现scrollViewDidZoom:委托方法并将两个转换连接起来以达到预期的效果:

    - (void) scrollViewDidZoom:(UIScrollView *) scrollView
    {
        CATransform3D scale = contentView.layer.transform;
        CATransform3D rotation = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
    
        contentView.layer.transform = CATransform3DConcat(rotation, scale);
    }
    

    编辑

    我有更简单的想法!如何在附加了旋转变换的情况下向层次结构添加另一个视图?这是建议的层次结构:

    • 滚动视图
      • ContentView - viewForZoomingInScrollView: 返回的那个
        • RotationView - 带有旋转变换的视图
          • MapView - 包含所有图块的视图

    我认为性能不应该是任何问题,值得一试。

    【讨论】:

    • 嗨,Bartosz,首先,谢谢你的帖子!但这对我不起作用......我已经编辑了锚点以围绕当前位置旋转。也许这就是问题所在?我将编辑我的帖子并添加该代码。
    • 所以它保留了旋转,但是旋转轴不对吧?
    • 我真的不知道。就我所见,它围绕一个奇怪的点旋转了很多。但轴似乎是对的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多