UIScrollView setZoomScale将应用的旋转设置回零 [英] UIScrollView setZoomScale sets the applied rotation back to Zero
问题描述
我已经进行了很长时间的地图替换工作。整个过程都由 UIScrollView
和 CATiledLayer
支持。
I've been working on a maps replacement for quite a while now. The whole thing works with a UIScrollView
backed by a CATiledLayer
.
要旋转地图,请旋转图层本身。 (使用 CATransform3DMakeRotation
到目前为止效果很好=)
To rotate my map, i rotate the layer itself. (Using CATransform3DMakeRotation
) Works pretty well so far =)
但是如果我曾经打电话给 setZoomScale
方法将要提交到我的图层的 CATransform3D
将旋转值重置为0。
But if I ever call setZoomScale
method the CATransform3D
that is going to be submitted to my layer is resetting the rotation to 0.
我的问题是,有什么方法可以设置我的scrollView的 zoomscale
而不会丢失所应用的旋转角度?
My question is, is there any way to set the zoomscale
of my scrollView without loosing the applied rotation?
捏捏手势也存在同样的问题。
The same problem exists for the pinch gestures.
//其他信息
要旋转当前位置,我必须编辑锚点。也许这也是缩放的问题。
To rotate around the current Position, i have to edit the anchor point. Maybe this is a problem for the scaling, too.
- (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:
委托方法,并连接两个转换以达到所需的效果:
You can implement scrollViewDidZoom:
delegate method and concatenate the two transforms to achieve desired effect:
- (void) scrollViewDidZoom:(UIScrollView *) scrollView
{
CATransform3D scale = contentView.layer.transform;
CATransform3D rotation = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
contentView.layer.transform = CATransform3DConcat(rotation, scale);
}
编辑
我有个简单的主意!如何在附加了旋转变换的情况下向层次结构添加另一个视图?这是建议的层次结构:
I've got simpler idea! How about adding another view to the hierarchy with the rotation transform attached? Here's the proposed hierarchy:
- ScrollView
- ContentView-<$返回的那个c $ c> viewForZoomingInScrollView:
- RotationView-具有旋转变换的对象
- MapView-带有所有图块的那个
我认为这里的性能并不重要,值得尝试。
I don't think that performance should be any concern here and it's worth trying.
这篇关于UIScrollView setZoomScale将应用的旋转设置回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- RotationView-具有旋转变换的对象
- ContentView-<$返回的那个c $ c> viewForZoomingInScrollView: