限制 CGAffineTransform 规模

发布于 2024-10-23 15:08:11 字数 1556 浏览 1 评论 0原文

我正在使用 CGAffineTransformMake 转换视图。它可以旋转、缩放和平移。这很好用。但我无法找到一种方法将比例限制为最大尺寸。

如果超出比例,我仍然需要应用当前的旋转和平移。

任何建议将不胜感激!

来源:

UITouch *touch1 = [sortedTouches objectAtIndex:0];
UITouch *touch2 = [sortedTouches objectAtIndex:1];

CGPoint beginPoint1 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch1);
CGPoint currentPoint1 = [touch1 locationInView:self.superview];
CGPoint beginPoint2 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch2);
CGPoint currentPoint2 = [touch2 locationInView:self.superview];

double layerX = self.center.x;
double layerY = self.center.y;

double x1 = beginPoint1.x - layerX;
double y1 = beginPoint1.y - layerY;
double x2 = beginPoint2.x - layerX;
double y2 = beginPoint2.y - layerY;
double x3 = currentPoint1.x - layerX;
double y3 = currentPoint1.y - layerY;
double x4 = currentPoint2.x - layerX;
double y4 = currentPoint2.y - layerY;

// Solve the system:
//   [a b t1, -b a t2, 0 0 1] * [x1, y1, 1] = [x3, y3, 1]
//   [a b t1, -b a t2, 0 0 1] * [x2, y2, 1] = [x4, y4, 1]

double D = (y1-y2)*(y1-y2) + (x1-x2)*(x1-x2);
if (D < 0.1) {
    return CGAffineTransformMakeTranslation(x3-x1, y3-y1);
}

double a = (y1-y2)*(y3-y4) + (x1-x2)*(x3-x4);
double b = (y1-y2)*(x3-x4) - (x1-x2)*(y3-y4);
double tx = (y1*x2 - x1*y2)*(y4-y3) - (x1*x2 + y1*y2)*(x3+x4) + x3*(y2*y2 + x2*x2) + x4*(y1*y1 + x1*x1);
double ty = (x1*x2 + y1*y2)*(-y4-y3) + (y1*x2 - x1*y2)*(x3-x4) + y3*(y2*y2 + x2*x2) + y4*(y1*y1 + x1*x1);

return   CGAffineTransformMake(a/D, -b/D, b/D, a/D, tx/D, ty/D);

I am transforming a view with CGAffineTransformMake. It rotates, scales and translates. This works just fine. But I cannot figure out a way to limit the scale to a maximum size.

If the scale is exceeded, I need to still apply the current rotation and translation.

Any suggestions are greatly appreciated!

Source:

UITouch *touch1 = [sortedTouches objectAtIndex:0];
UITouch *touch2 = [sortedTouches objectAtIndex:1];

CGPoint beginPoint1 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch1);
CGPoint currentPoint1 = [touch1 locationInView:self.superview];
CGPoint beginPoint2 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch2);
CGPoint currentPoint2 = [touch2 locationInView:self.superview];

double layerX = self.center.x;
double layerY = self.center.y;

double x1 = beginPoint1.x - layerX;
double y1 = beginPoint1.y - layerY;
double x2 = beginPoint2.x - layerX;
double y2 = beginPoint2.y - layerY;
double x3 = currentPoint1.x - layerX;
double y3 = currentPoint1.y - layerY;
double x4 = currentPoint2.x - layerX;
double y4 = currentPoint2.y - layerY;

// Solve the system:
//   [a b t1, -b a t2, 0 0 1] * [x1, y1, 1] = [x3, y3, 1]
//   [a b t1, -b a t2, 0 0 1] * [x2, y2, 1] = [x4, y4, 1]

double D = (y1-y2)*(y1-y2) + (x1-x2)*(x1-x2);
if (D < 0.1) {
    return CGAffineTransformMakeTranslation(x3-x1, y3-y1);
}

double a = (y1-y2)*(y3-y4) + (x1-x2)*(x3-x4);
double b = (y1-y2)*(x3-x4) - (x1-x2)*(y3-y4);
double tx = (y1*x2 - x1*y2)*(y4-y3) - (x1*x2 + y1*y2)*(x3+x4) + x3*(y2*y2 + x2*x2) + x4*(y1*y1 + x1*x1);
double ty = (x1*x2 + y1*y2)*(-y4-y3) + (y1*x2 - x1*y2)*(x3-x4) + y3*(y2*y2 + x2*x2) + y4*(y1*y1 + x1*x1);

return   CGAffineTransformMake(a/D, -b/D, b/D, a/D, tx/D, ty/D);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不念旧人 2024-10-30 15:08:12

做这样的事情:

CGAffineTransform transform = self.view.transform;
float scale = sqrt(transform.a*transform.a + transform.c*transform.c);
if (scale > SCALE_MAX)
    self.view.transform = CGAffineTransformScale(transform, SCALE_MAX/scale, SCALE_MAX/scale);
else if (scale < SCALE_MIN)
    self.view.transform = CGAffineTransformScale(transform, SCALE_MIN/scale, SCALE_MIN/scale);

在你的 TouchMoved:withEvent: 和 updateOriginalTransformForTouches 方法的末尾。基本上,您检查当前比例是否超过某个 SCALE_MAX 值,然后将转换矩阵与反转的比例值相乘。

Do something like this:

CGAffineTransform transform = self.view.transform;
float scale = sqrt(transform.a*transform.a + transform.c*transform.c);
if (scale > SCALE_MAX)
    self.view.transform = CGAffineTransformScale(transform, SCALE_MAX/scale, SCALE_MAX/scale);
else if (scale < SCALE_MIN)
    self.view.transform = CGAffineTransformScale(transform, SCALE_MIN/scale, SCALE_MIN/scale);

At the end of your touchesMoved:withEvent: and updateOriginalTransformForTouches methods. Basically, you check if the current scale exceeds a certain SCALE_MAX value, then multiply your transformation matrix with the reversed scale value.

江挽川 2024-10-30 15:08:12

如果有人需要 @Enzo Tran 在 Swift 中使用 UIPanGestureRecognizer 的答案:

func handlePinch(recognizer : UIPinchGestureRecognizer) {
    if let view = recognizer.view {
        view.transform = CGAffineTransformScale(view.transform,
                                                   recognizer.scale, recognizer.scale)
        let transform = view.transform
        let maxScale: CGFloat = 1.7 //Anything
        let minScale: CGFloat = 0.5 //Anything
        let scale = sqrt(transform.a * transform.a + transform.c * transform.c)
        if scale > maxScale {
            view.transform = CGAffineTransformScale(transform, maxScale / scale, maxScale / scale)
        }
        else if scale < minScale {
            view.transform = CGAffineTransformScale(transform, minScale / scale, minScale / scale)
        }

        recognizer.scale = 1
    }
} 

In case anyone needs @Enzo Tran's answer in Swift with UIPanGestureRecognizer:

func handlePinch(recognizer : UIPinchGestureRecognizer) {
    if let view = recognizer.view {
        view.transform = CGAffineTransformScale(view.transform,
                                                   recognizer.scale, recognizer.scale)
        let transform = view.transform
        let maxScale: CGFloat = 1.7 //Anything
        let minScale: CGFloat = 0.5 //Anything
        let scale = sqrt(transform.a * transform.a + transform.c * transform.c)
        if scale > maxScale {
            view.transform = CGAffineTransformScale(transform, maxScale / scale, maxScale / scale)
        }
        else if scale < minScale {
            view.transform = CGAffineTransformScale(transform, minScale / scale, minScale / scale)
        }

        recognizer.scale = 1
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文