放大后平移UIScrollView的子视图

时间:2013-11-04 02:38:47

标签: ios objective-c uiscrollview

我在UIScrollView中添加了一个子视图。当我放大滚动视图时,我想在子视图中平移。

touchesBegan:我获得了触摸的初始位置,然后touchesMoved:我能够确定移动子视图的数量。当zoomscale为1.0时,它可以正常工作。但是,当它被缩放时,指针会“打破”它想要移动的子视图(这里的插图 - 指针位置被定义为选框工具)。

视图的中心应位于指针位置,而不是位于当前位置! px和py变量确保单击子视图上的任何位置,在拖动它时,指针的位置始终保持不变。 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;
    }

}

1 个答案:

答案 0 :(得分:1)

而不是你正在采取的方法,使子视图成为另一个UIScrollView并让它处理平移。

(您可能希望在子视图上设置scrollEnabled = NO,直到进行缩放。)