Browse files

Change the implementation of our roundrect one with something a touch…

… superior.
  • Loading branch information...
1 parent 503476f commit 28b742eab5174d20c30f54d4169dbb297a19b49a Danny Greg committed May 24, 2012
Showing with 55 additions and 19 deletions.
  1. +15 −1 lib/UIKit/TUICGAdditions.h
  2. +39 −17 lib/UIKit/TUICGAdditions.m
  3. +1 −1 lib/UIKit/TUIProgressBar.m
View
16 lib/UIKit/TUICGAdditions.h
@@ -14,14 +14,28 @@
limitations under the License.
*/
+enum _TUICGRoundedRectCorner {
+ TUICGRoundedRectCornerTopLeft = 1 << 0,
+ TUICGRoundedRectCornerTopRight = 1 << 1,
+ TUICGRoundedRectCornerBottomLeft = 1 << 2,
+ TUICGRoundedRectCornerBottomRight = 1 << 3,
+ TUICGRoundedRectCornerTop = TUICGRoundedRectCornerTopLeft | TUICGRoundedRectCornerTopRight,
+ TUICGRoundedRectCornerBottom = TUICGRoundedRectCornerBottomLeft | TUICGRoundedRectCornerBottomRight,
+ TUICGRoundedRectCornerAll = TUICGRoundedRectCornerTopLeft | TUICGRoundedRectCornerTopRight | TUICGRoundedRectCornerBottomLeft | TUICGRoundedRectCornerBottomRight,
+ TUICGRoundedRectCornerNone = 0,
+};
+
+typedef NSUInteger TUICGRoundedRectCorner;
+
#import <Foundation/Foundation.h>
extern CGContextRef TUICreateOpaqueGraphicsContext(CGSize size);
extern CGContextRef TUICreateGraphicsContext(CGSize size);
extern CGContextRef TUICreateGraphicsContextWithOptions(CGSize size, BOOL opaque);
extern CGImageRef TUICreateCGImageFromBitmapContext(CGContextRef ctx);
-CGPathRef TUICreateRoundRectInRectWithRadius(CGRect rect, CGFloat radius);
+extern CGPathRef TUICGPathCreateRoundedRect(CGRect rect, CGFloat radius);
+extern CGPathRef TUICGPathCreateRoundedRectWithCorners(CGRect rect, CGFloat radius, TUICGRoundedRectCorner corners);
extern void CGContextAddRoundRect(CGContextRef context, CGRect rect, CGFloat radius);
extern void CGContextClipToRoundRect(CGContextRef context, CGRect rect, CGFloat radius);
View
56 lib/UIKit/TUICGAdditions.m
@@ -57,29 +57,51 @@ CGImageRef TUICreateCGImageFromBitmapContext(CGContextRef ctx) // autoreleased
return CGBitmapContextCreateImage(ctx);
}
-CGPathRef TUICreateRoundRectInRectWithRadius(CGRect rect, CGFloat radius)
-{
- radius = MIN(radius, rect.size.width / 2);
- radius = MIN(radius, rect.size.height / 2);
- radius = floor(radius);
+CGPathRef TUICGPathCreateRoundedRect(CGRect rect, CGFloat radius) {
+ return TUICGPathCreateRoundedRectWithCorners(rect, radius, TUICGRoundedRectCornerAll);
+}
+
+CGPathRef TUICGPathCreateRoundedRectWithCorners(CGRect rect, CGFloat radius, TUICGRoundedRectCorner corners) {
+ CGMutablePathRef path = CGPathCreateMutable();
+ CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y + radius);
+ CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height - radius);
+
+ if((corners & TUICGRoundedRectCornerTopLeft) != 0) {
+ CGPathAddArc(path, NULL, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);
+ } else {
+ CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height);
+ }
+
+ CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height);
+
+ if((corners & TUICGRoundedRectCornerTopRight) != 0) {
+ CGPathAddArc(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
+ } else {
+ CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
+ }
+
+ CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + radius);
+
+ if((corners & TUICGRoundedRectCornerBottomRight) != 0) {
+ CGPathAddArc(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI / 2, 1);
+ } else {
+ CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y);
+ }
+
+ CGPathAddLineToPoint(path, NULL, rect.origin.x + radius, rect.origin.y);
- CGMutablePathRef returnPath = CGPathCreateMutable();
- CGPathMoveToPoint(returnPath, NULL, rect.origin.x, rect.origin.y + radius);
- CGPathAddLineToPoint(returnPath, NULL, rect.origin.x, rect.origin.y + rect.size.height - radius);
- CGPathAddArc(returnPath, NULL, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);
- CGPathAddLineToPoint(returnPath, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height);
- CGPathAddArc(returnPath, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
- CGPathAddLineToPoint(returnPath, NULL, rect.origin.x + rect.size.width, rect.origin.y + radius);
- CGPathAddArc(returnPath, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI / 2, 1);
- CGPathAddLineToPoint(returnPath, NULL, rect.origin.x + radius, rect.origin.y);
- CGPathAddArc(returnPath, NULL, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI / 2, M_PI, 1);
+ if((corners & TUICGRoundedRectCornerBottomLeft) != 0) {
+ CGPathAddArc(path, NULL, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI / 2, M_PI, 1);
+ } else {
+ CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y);
+ }
- return returnPath;
+ return path;
}
void CGContextAddRoundRect(CGContextRef context, CGRect rect, CGFloat radius)
{
- CGPathRef path = TUICreateRoundRectInRectWithRadius(rect, radius);
+ CGPathRef path = TUICGPathCreateRoundedRect(rect, radius);
CGContextAddPath(context, path);
CGPathRelease(path);
}
View
2 lib/UIKit/TUIProgressBar.m
@@ -268,7 +268,7 @@ - (void)setIndeterminate:(BOOL)indeterminate
animationClippingView.opaque = NO;
animationClippingView.backgroundColor = [TUIColor clearColor];
- CGPathRef clipPath = TUICreateRoundRectInRectWithRadius(animationClippingView.bounds, ceil(NSHeight(animationClippingView.bounds) / 2.0));
+ CGPathRef clipPath = TUICGPathCreateRoundedRect(animationClippingView.bounds, ceil(NSHeight(animationClippingView.bounds) / 2.0));
CAShapeLayer *clipLayer = [[CAShapeLayer alloc] init];
clipLayer.path = clipPath;
animationClippingView.layer.mask = clipLayer;

0 comments on commit 28b742e

Please sign in to comment.