Permalink
Browse files

Clip the progressbar's barber poles so they no longer overspill.

  • Loading branch information...
1 parent 03d370f commit 0d38cbc90c1ff5c384b09e8e0a4084e16e78d5ab Danny Greg committed May 24, 2012
Showing with 27 additions and 10 deletions.
  1. +1 −0 lib/UIKit/TUICGAdditions.h
  2. +20 −10 lib/UIKit/TUICGAdditions.m
  3. +6 −0 lib/UIKit/TUIProgressBar.m
@@ -21,6 +21,7 @@ extern CGContextRef TUICreateGraphicsContext(CGSize size);
extern CGContextRef TUICreateGraphicsContextWithOptions(CGSize size, BOOL opaque);
extern CGImageRef TUICreateCGImageFromBitmapContext(CGContextRef ctx);
+CGPathRef TUICreateRoundRectInRectWithRadius(CGRect rect, CGFloat radius);
extern void CGContextAddRoundRect(CGContextRef context, CGRect rect, CGFloat radius);
extern void CGContextClipToRoundRect(CGContextRef context, CGRect rect, CGFloat radius);
View
@@ -57,21 +57,31 @@ CGImageRef TUICreateCGImageFromBitmapContext(CGContextRef ctx) // autoreleased
return CGBitmapContextCreateImage(ctx);
}
-void CGContextAddRoundRect(CGContextRef context, CGRect rect, CGFloat radius)
+CGPathRef TUICreateRoundRectInRectWithRadius(CGRect rect, CGFloat radius)
{
radius = MIN(radius, rect.size.width / 2);
radius = MIN(radius, rect.size.height / 2);
radius = floor(radius);
- CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
- CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
- CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);
- CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height);
- CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
- CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
- CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI / 2, 1);
- CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
- CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI / 2, M_PI, 1);
+ 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);
+
+ return returnPath;
+}
+
+void CGContextAddRoundRect(CGContextRef context, CGRect rect, CGFloat radius)
+{
+ CGPathRef path = TUICreateRoundRectInRectWithRadius(rect, radius);
+ CGContextAddPath(context, path);
+ CGPathRelease(path);
}
void CGContextClipToRoundRect(CGContextRef context, CGRect rect, CGFloat radius)
@@ -268,6 +268,12 @@ - (void)setIndeterminate:(BOOL)indeterminate
animationClippingView.opaque = NO;
animationClippingView.backgroundColor = [TUIColor clearColor];
+ CGPathRef clipPath = TUICreateRoundRectInRectWithRadius(animationClippingView.bounds, ceil(NSHeight(animationClippingView.bounds) / 2.0));
+ CAShapeLayer *clipLayer = [[CAShapeLayer alloc] init];
+ clipLayer.path = clipPath;
+ animationClippingView.layer.mask = clipLayer;
+ CGPathRelease(clipPath);
+
CGRect animationViewFrame = CGRectMake(NSMinX(animationClippingView.bounds),NSMinY(animationClippingView.bounds), (NSWidth(animationClippingView.bounds) * 2.0), NSHeight(animationClippingView.bounds));
self.animationView = [[TUIView alloc] initWithFrame:animationViewFrame];
self.animationView.opaque = NO;

0 comments on commit 0d38cbc

Please sign in to comment.