單點讓UIView同時縮放與旋轉


UITouch *touch1 = [sortedTouches objectAtIndex:0]; //觸控移動的點
        CGPoint beginPoint1 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch1);
        CGPoint currentPoint1 = [touch1 locationInView:self.superview];

        double layerX = self.center.x;//指定UIView的中心點
        double layerY = self.center.y;//指定UIView的中心點

        double x1 = beginPoint1.x - layerX;
        double y1 = beginPoint1.y - layerY;

        double x2 = layerX - layerX;
        double y2 = layerY - layerY;

        double x3 = currentPoint1.x - layerX;
        double y3 = currentPoint1.y - layerY;

        double x4 = layerX - layerX;
        double y4 = layerY - layerY;
        
        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);
        
self.transform = CGAffineTransformMake(a/D, -b/D, b/D, a/D, tx/D, ty/D);//把變動過後的矩陣給View的

留言

這個網誌中的熱門文章

[心得] 圖解 微分、積分生活中的微積分-第一章

Objective-C的數學運算函式

c# 把List中重復的資料去掉