Iphone 极限四维转换尺度
我正在使用CGAffineTransformMake转换视图。它旋转、缩放和平移。这个很好用。但我想不出一个办法来限制规模的最大规模 如果超出比例,我仍然需要应用当前的旋转和平移 非常感谢您的任何建议 资料来源:Iphone 极限四维转换尺度,iphone,scale,multi-touch,uitouch,cgaffinetransform,Iphone,Scale,Multi Touch,Uitouch,Cgaffinetransform,我正在使用CGAffineTransformMake转换视图。它旋转、缩放和平移。这个很好用。但我想不出一个办法来限制规模的最大规模 如果超出比例,我仍然需要应用当前的旋转和平移 非常感谢您的任何建议 资料来源: UITouch *touch1 = [sortedTouches objectAtIndex:0]; UITouch *touch2 = [sortedTouches objectAtIndex:1]; CGPoint beginPoint1 = *(CGPoint *)CFDict
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);
UITouch*touch1=[sortedTouches对象索引:0];
UITouch*touch2=[sortedTouches对象索引: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];
双层x=自中心x;
双层=self.center.y;
双x1=起始点1.x-层Rx;
双y1=起点1.y-分层;
双x2=起始点2.x-层x;
双y2=起始点2.y-分层;
双x3=currentPoint1.x-layerX;
双y3=电流点1.y-分层;
双x4=currentPoint2.x-layerX;
双y4=电流点2.y-分层;
//解决系统问题:
//[ABT1,-BAT2,01]*[x1,y1,1]=[x3,y3,1]
//[ABT1,-BAT2,01]*[x2,y2,1]=[x4,y4,1]
双D=(y1-y2)*(y1-y2)+(x1-x2)*(x1-x2);
如果(D<0.1){
返回CGAffineTransformMakeTransform(x3-x1,y3-y1);
}
双a=(y1-y2)*(y3-y4)+(x1-x2)*(x3-x4);
双b=(y1-y2)*(x3-x4)-(x1-x2)*(y3-y4);
双tx=(y1*x2-x1*y2)*(y4-y3)-(x1*x2+y1*y2)*(x3+x4)+x3*(y2*y2+x2*x2)+x4*(y1*y1+x1*x1);
双y=(x1*x2+y1*y2)*(-y4-y3)+(y1*x2-x1*y2)*(x3-x4)+y3*(y2*y2+x2*x2)+y4*(y1*y1+x1*x1);
返回CGAffineTransformMake(a/D,-b/D,b/D,a/D,tx/D,ty/D);
执行以下操作:
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);
CGAffineTransform transform=self.view.transform;
浮动比例=sqrt(transform.a*transform.a+transform.c*transform.c);
如果(缩放>缩放最大值)
self.view.transform=CGAffineTransformScale(变换,缩放最大/缩放,缩放最大/缩放);
否则如果(刻度<刻度最小值)
self.view.transform=CGAffineTransformScale(变换、缩放最小/缩放、缩放最小/缩放);
在触摸结束时,Moved:withEvent:和updateOriginalTransformForTouches方法。基本上,您可以检查当前的比例是否超过某个比例的最大值,然后将变换矩阵与反向的比例值相乘。如果有人需要使用UIPangestureRecognitizer在Swift中提供@Enzo Tran的答案:
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
}
}
func handlePinch(识别器:UIPinchGestureRecognizer){
如果let view=recognizer.view{
view.transform=CGAffineTransformScale(view.transform,
识别器。比例,识别器。比例)
让transform=view.transform
设maxScale:CGFloat=1.7//
让minScale:CGFloat=0.5//
设scale=sqrt(transform.a*transform.a+transform.c*transform.c)
如果比例>最大比例{
view.transform=CGAffineTransformScale(变换,最大尺度/尺度,最大尺度/尺度)
}
如果标度<最小标度,则为else{
view.transform=CGAffineTransformScale(变换、最小刻度/刻度、最小刻度/刻度)
}
识别器.scale=1
}
}
完美!!!非常感谢你!我不能投票赞成你的答案,因为我没有足够的声誉。有人能帮我投票吗?如果可以的话,我会投10票!再次感谢。顺便说一句:我有一篇线性代数课文,我正在努力学习这类东西。它也非常适合我的情况,如果是现代实现(例如:iOS 5.0及以上),请尝试使用UIPangestureRecognitor。