我从事 map 替换工作已经有一段时间了。整个过程与一个由 CATiledLayer
支持的 UIScrollView
一起工作。
为了旋转我的 map ,我旋转了图层本身。 (使用 CATransform3DMakeRotation
)到目前为止效果很好 =)
但是,如果我曾经调用 setZoomScale
方法,将要提交到我的层的 CATransform3D
会将旋转重置为 0。
我的问题是,有没有什么方法可以设置我的 scrollView 的 zoomscale
而不会丢失应用的旋转?
捏合手势也存在同样的问题。
//附加信息
要围绕当前位置旋转,我必须编辑 anchor 。也许这也是缩放的问题。
- (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];
}
}
最佳答案
您可以实现 scrollViewDidZoom:
委托(delegate)方法并连接两个转换以达到预期效果:
- (void) scrollViewDidZoom:(UIScrollView *) scrollView
{
CATransform3D scale = contentView.layer.transform;
CATransform3D rotation = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
contentView.layer.transform = CATransform3DConcat(rotation, scale);
}
编辑
我有更简单的想法!如何将另一个 View 添加到附加了旋转变换的层次结构中?这是建议的层次结构:
- ScrollView
- ContentView -
viewForZoomingInScrollView:
返回的那个- RotationView - 带有旋转变换的那个
- MapView - 包含所有图 block 的 View
- RotationView - 带有旋转变换的那个
- ContentView -
我不认为性能应该是这里的任何问题,值得一试。
关于ios - UIScrollView setZoomScale 将应用的旋转设置回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12006171/