我正在使用 UIPinchGestureRecognizer
来调整 UIScrollView
中 View 的宽度(不是高度)。它与捏合手势的 scale
属性一起使用,但 ScrollView 的 contentOffset
不会改变,因此 View 始终在右侧增加。如果我随宽度一起缩放 contentOffset
,这看起来会好一些,因为那时 View 会从屏幕的最左侧增加。
问题是捏合的位置被忽略了——所以捏合总是出现在屏幕的左侧。
我需要以某种方式将夹点位置考虑到 contentOffset
调整中,以便可以调整偏移量以将夹点处的内容保持在同一位置。
注意:我不能使用内置的 UIScrollView
捏拉缩放手势,因为我只希望缩放是一维的,水平的。另外,我不能在 UIView
上使用转换,因为我需要使用 UIScrollView
。
最佳答案
我正在捏缩放图表,所以捏调整图 TableView 的宽度约束。
这是夹点处理程序:
- (void) doPinch:(UIPinchGestureRecognizer*)pinch;
{
CGFloat width = self.graphWidthConstraint.constant;
CGFloat idealWidth = 1500;
CGFloat currentScale = width / idealWidth;
CGFloat scale = currentScale - (1.0 - pinch.scale);
CGFloat minScale = 0.5;
CGFloat maxScale = 3.0;
scale = MIN(scale, maxScale);
scale = MAX(scale, minScale);
CGPoint locationScrollview = [pinch locationInView:self.graphScrollView];
CGFloat pinchXNormalized = locationScrollview.x / width;
CGPoint locationView = [pinch locationInView:self.view];
// resize
CGFloat newWidth = idealWidth * scale;
self.graphWidthConstraint.constant = newWidth;
// set offset to position point under touch
CGFloat pinchXScaled = newWidth * pinchXNormalized;
CGFloat x = pinchXScaled - locationView.x;
self.graphScrollView.contentOffset = CGPointMake(x, 0);
pinch.scale = 1;
}
关于ios - UIScrollView 中的水平缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24521809/