首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >限制CGAffineTransform规模

限制CGAffineTransform规模
EN

Stack Overflow用户
提问于 2011-03-19 09:56:38
回答 2查看 5.1K关注 0票数 2

我正在用CGAffineTransformMake转换视图。它可以旋转、缩放和平移。这个很好用。但我想不出一种方法将规模限制在最大尺寸。

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

任何建议都是非常感谢的!

来源:

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-21 21:05:49

这样做:

代码语言:javascript
复制
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);

在触摸的最后methods :withEvent:和updateOriginalTransformForTouches方法。基本上,您检查当前比例是否超过某个SCALE_MAX值,然后将转换矩阵乘以反转的比例值。

票数 36
EN

Stack Overflow用户

发布于 2016-10-16 10:42:53

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

代码语言:javascript
复制
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
    }
} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5359504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档