Note that there are some explanatory texts on larger screens.

plurals
  1. POClear remnants of drawRect() in custom UIControl
    text
    copied!<p>I create a custom slider with using subclass of UIControl, i draw my background and button in drawRect() but when i redraw my button, they is remnants of previous draw, how can i clear this ?? I try to track memory leaks with instruments but no leaks. my code:</p> <pre><code>- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGFloat radius = 10; // [[UIColor redColor] set]; // CGContextAddRect(context, rect); // CGContextStrokePath(context); // Make sure corner radius isn't larger than half the shorter side if (radius &gt; self.bounds.size.width/2.0) radius = self.bounds.size.width/2.0; if (radius &gt; self.bounds.size.height/2.0) radius = self.bounds.size.height/2.0; CGFloat minx = CGRectGetMinX(self.bounds); CGFloat midx = CGRectGetMidX(self.bounds); CGFloat maxx = CGRectGetMaxX(self.bounds); CGFloat miny = CGRectGetMinY(self.bounds); CGFloat maxy = CGRectGetMaxY(self.bounds); // CGContextMoveToPoint(context, minx, midy); // CGContextAddArcToPoint(context, minx, miny, midx, miny, radius); // CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius); // CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); // CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius); CGContextMoveToPoint(context, maxx, miny); CGContextAddArcToPoint(context, minx, miny+ 30, minx, miny+150, radius); CGContextAddArcToPoint(context, minx, maxy-30, minx+30, maxy, radius); CGContextAddLineToPoint (context, maxx, maxy); CGContextClosePath(context); CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.2 ); CGContextSetRGBStrokeColor( context, 0.7, 0.7, 0.7, 0.4 ); CGContextDrawPath(context, kCGPathFill); CGContextSetLineWidth(context, 10); CGContextSetLineCap(context, kCGLineCapRound); CGContextMoveToPoint(context, midx, miny+ 80); CGContextAddLineToPoint(context, midx, maxy-50); CGContextStrokePath(context); // CGContextDrawPath(context, kCGPathStroke); [self drawButton: _buttonRect inContext:context]; } </code></pre> <p>and drawButton() function </p> <pre><code>- (void)drawButton:(CGRect)rect inContext:(CGContextRef) context{ UIGraphicsPushContext(context); CGFloat radius = 7; // [[UIColor redColor] set]; // CGContextAddRect(context, rect); // CGContextStrokePath(context); // if (radius &gt; self.bounds.size.width/2.0) radius = self.bounds.size.width/2.0; // if (radius &gt; self.bounds.size.height/2.0) radius = self.bounds.size.height/2.0; CGFloat minx = CGRectGetMinX(rect); CGFloat midx = CGRectGetMidX(rect); CGFloat maxx = CGRectGetMaxX(rect); CGFloat miny = CGRectGetMinY(rect); CGFloat midy = CGRectGetMidY(rect); CGFloat maxy = CGRectGetMaxY(rect); CGContextMoveToPoint(context, minx, midy); CGContextAddArcToPoint(context, minx, miny, midx, miny, radius); CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius); CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius); CGContextClosePath(context); CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0 ); // CGContextSetRGBStrokeColor(context, // 0.0, 0.0, 0.0, 1.0 ); CGContextDrawPath(context, kCGPathFill); CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0 ); //Gradient related variables size_t locationCount = 3; CGFloat locationList[] = {0.0,0.5,1.0}; CGFloat colorList[] = { 0.0, 0.0, 0.0, 1.0, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0 }; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colorList, locationList, locationCount); //Paint a linear gradient CGPoint startPoint, endPoint; startPoint.x = midx; startPoint.y = midy-2.5; endPoint.x = midx; endPoint.y = midy+2.5; CGContextSaveGState(context); CGContextAddEllipseInRect(context, CGRectMake(midx-2.5, midy-2.5, 5, 5)); CGContextClosePath(context); CGContextClip(context); CGContextDrawLinearGradient (context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation); CGContextRestoreGState(context); UIGraphicsPopContext(); } </code></pre> <p>i try a lot of things but now i had no idea, thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload