我已经向 UIScrollView 添加了一个 subview 。当我放大 ScrollView 时,我想在 subview 周围平移。
在 touchesBegan:
中,我得到了触摸的初始位置,然后在 touchesMoved:
中,我能够确定 subview 要移动多少。它在 zoomscale
为 1.0 时完美运行。但是,当它被缩放时,指针会“脱离”它打算移动的 subview (此处的插图 - 指针位置被显示为选取框工具)。
View 的中心应该位于指针位置,而不是当前位置! px 和 py 变量确保 subview 上的任何地方被点击,而拖动它时指针的位置始终保持不变。 illustration
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
location.x = location.x * self.zoomScale;
location.y = location.y * self.zoomScale;
px = location.x;
py = location.y;
if ([touch view] == rotateView) {
self.scrollEnabled = NO;
return;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
location.x = location.x * self.zoomScale;
location.y = location.y * self.zoomScale;
if ([touch view] == rotateView) {
rotateView.center = CGPointMake(rotateView.center.x + (location.x - px), rotateView.center.y + (location.y - py));
px = location.x;
py = location.y;
return;
}
}
最佳答案
代替您采用的方法,将 subview 设为另一个 UIScrollView 并让它处理平移。
(您可能希望在您的 subview 上设置 scrollEnabled = NO
,直到发生缩放。)
关于ios - 放大后平移 UIScrollView 的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19760846/