ios - UIScrollView 中的水平缩放

标签 ios uiscrollview uipinchgesturerecognizer contentoffset

我正在使用 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/

相关文章:

ios - 使用捏合手势调整 Sprite Kit 节点大小

ios - Marmalade SDK 是否更适合或更常用于游戏开发?

ios - 如何滚动到 UIScrollview 中的 UIButton 位置

ios - 在后台线程上执行核心数据保存?

ios - 无法滚动到 UIScrollView 中嵌入的 UITableView 的顶部

iphone - 在 UIScrollView 中缩放和滚动大图像

ios - UIPinchGestureRecognizer 仅用于捏合

iphone - 如何在iphone中获取pinchgesture的坐标

ios - 如何让 tableView.reloadData() 与核心数据正常工作?

php - IOS 连接到 PHP webserver 插入数据到 mysql