I want to create "rounded" "semi-transparent" UIView (or anything does similar) my code below results in animation effects, gradually change transparent density. What I want is to avoid this effects and draw the resulting image straight from the beginning.
Can anyone know how to do? Thank you.
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
// Initialization code
super.opaque = NO;
self.alpha = 1.0f;
self.strokeColor = kDefaultStrokeColor;
super.backgroundColor = [UIColor clearColor];
self.rectColor = kDefaultRectColor;
self.strokeWidth = kDefaultStrokeWidth;
self.cornerRadius = kDefaultCornerRadius;
self.contentMode = UIViewContentModeRedraw;
self.clearsContextBeforeDrawing = NO;
}
return self;
}
- (void)setBackgroundColor:(UIColor *)newBGColor
{
// Ignore attempt to set backgroundColor
}
- (void)setOpaque:(BOOL)newIsOpaque
{
// Ignore attempt to set opaque to YES.
}
- (void)drawRect:(CGRect)rect {
BOOL areEnabled = [UIView areAnimationsEnabled];
// with or without setAnimationsEnabled:NO gives me the same result
[UIView setAnimationsEnabled:NO];
// with or without beginAnimations and commitAnimations gives me the same result
[UIView beginAnimations:@"test" context:nil];
[UIView setAnimationRepeatCount:1];
// [UIView setAnimationBeginsFromCurrentState:YES];
// With or without setAnimationDelay gives me the same result
//[UIView setAnimationDelay:0.0f];
// With or without setAnimationDuration gives me the same result
// passing 0.0f the same
//[UIView setAnimationDuration:0.1f];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSetLineWidth(context, strokeWidth);
CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
CGContextSetFillColorWithColor(context, self.rectColor.CGColor);
CGRect rrect = self.bounds;
CGFloat radius = cornerRadius;
CGFloat width = CGRectGetWidth(rrect);
CGFloat height = CGRectGetHeight(rrect);
// Make sure corner radius isn't larger than half the shorter side
if (radius > width/2.0)
radius = width/2.0;
if (radius > height/2.0)
radius = height/2.0;
CGFloat minx = CGRectGetMinX(rrect);
CGFloat midx = CGRectGetMidX(rrect);
CGFloat maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect);
CGFloat midy = CGRectGetMidY(rrect);
CGFloat maxy = CGRectGetMaxY(rrect);
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);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextClip(context);
[UIView commitAnimations];
[UIView setAnimationsEnabled:areEnabled];
// I am stacked...
}