Trích từ Wikipedia: Superellipse
Đối với n = 1/2, đặc biệt, mỗi trong số bốn vòng cung là một đường cong bậc hai Bézier xác định bởi hai trục; kết quả là, mỗi cung là một phân đoạn của một parabol.
Vậy tại sao không cố gắng gần đúng Squircle bằng đường cong Bezier? Cả hai đường cong (Bezier và Squircle) đều được xác định bởi các phương trình tham số.
UIBezierPath Class có phương pháp: addCurveToPoint:controlPoint1:controlPoint2:
Gắn một đường cong Bézier khối để đường đi của người nhận.
LƯU Ý: Sử dụng phương pháp addQuadCurveToPoint:controlPoint:
cho kết quả kém hơn - được kiểm tra.
tôi đã sử dụng phương pháp này và đó là những gì đã xảy ra kết quả là:
red line
- hình chữ nhật tròn, blue line
- Hình chữ nhật từ fours Bezier đường cong
Nếu kết quả này là quan tâm - vẽ mã bên dưới .
LƯU Ý: Để đạt được đường cong Bezier khớp chính xác hơn có thể được yêu cầu để thay đổi tọa độ của bốn corner points
(bây giờ chúng tương ứng với các góc của hình chữ nhật trong đó được ghi hình).
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
//set rect size for draw
float rectSize = 275.;
CGRect rectangle = CGRectMake(CGRectGetMidX(rect) - rectSize/2, CGRectGetMidY(rect) - rectSize/2, rectSize, rectSize);
//Rounded rectangle
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
UIBezierPath* roundedPath = [UIBezierPath bezierPathWithRoundedRect:rectangle cornerRadius:rectSize/4.7];
[roundedPath stroke];
//Rectangle from Fours Bezier Curves
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
UIBezierPath *bezierCurvePath = [UIBezierPath bezierPath];
//set coner points
CGPoint topLPoint = CGPointMake(CGRectGetMinX(rectangle), CGRectGetMinY(rectangle));
CGPoint topRPoint = CGPointMake(CGRectGetMaxX(rectangle), CGRectGetMinY(rectangle));
CGPoint botLPoint = CGPointMake(CGRectGetMinX(rectangle), CGRectGetMaxY(rectangle));
CGPoint botRPoint = CGPointMake(CGRectGetMaxX(rectangle), CGRectGetMaxY(rectangle));
//set start-end points
CGPoint midRPoint = CGPointMake(CGRectGetMaxX(rectangle), CGRectGetMidY(rectangle));
CGPoint botMPoint = CGPointMake(CGRectGetMidX(rectangle), CGRectGetMaxY(rectangle));
CGPoint topMPoint = CGPointMake(CGRectGetMidX(rectangle), CGRectGetMinY(rectangle));
CGPoint midLPoint = CGPointMake(CGRectGetMinX(rectangle), CGRectGetMidY(rectangle));
//Four Bezier Curve
[bezierCurvePath moveToPoint:midLPoint];
[bezierCurvePath addCurveToPoint:topMPoint controlPoint1:topLPoint controlPoint2:topLPoint];
[bezierCurvePath moveToPoint:midLPoint];
[bezierCurvePath addCurveToPoint:botMPoint controlPoint1:botLPoint controlPoint2:botLPoint];
[bezierCurvePath moveToPoint:midRPoint];
[bezierCurvePath addCurveToPoint:topMPoint controlPoint1:topRPoint controlPoint2:topRPoint];
[bezierCurvePath moveToPoint:midRPoint];
[bezierCurvePath addCurveToPoint:botMPoint controlPoint1:botRPoint controlPoint2:botRPoint];
[bezierCurvePath stroke];
CGContextRestoreGState(context);
Tôi không biết cách tạo hình chữ nhật tròn; Tôi thực sự là siêu sao của loại hình tròn, khi chúng sử dụng cho biểu tượng springboard trên iOS 7. –
@RemyVanherweghem Phương pháp 'bezierPathWithRoundedRect' đã được sửa đổi trong iOS 7 để vẽ các góc mượt mà hơn. Ngoài ra nó không có vẻ là một hình vuông: http://blog.mikeswanson.com/post/62341902567/unleashing-genetic-algorithms-on-the-ios-7-icon – millimoose
(Điều đó nói rằng, phương thức 'UIBezierPath' không tạo ra một kết hợp hoàn hảo với mẫu biểu tượng, chúng chỉ gần hơn so với trước đây.) – millimoose