Reputation: 6384
I'm trying to make multiple circles within a UIView. I can get one to display on the screen but not more. I replaced the circle class with a UILabel and outputted the index of my loop and the logic worked fine, so I'm not sure what the error would be with a UIView subclass. Here's the relevant code:
FEG_Circle (UIView subclass)
.h
@property (nonatomic, assign) float radius;
@property (nonatomic, retain) UIColor* clrCircle;
- (void) setMyCircle : (UIColor*) clrCircle : (float) radius;
.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void) setMyCircle : (UIColor*) clrCircle : (float) radius {
_clrCircle = clrCircle;
_radius = radius;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
if(_clrCircle) {
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 2.0);
CGContextSetFillColorWithColor(contextRef, _clrCircle.CGColor);
CGRect circlePoint = (CGRectMake(self.frame.origin.x, self.frame.origin.y, _radius, _radius));
CGContextFillEllipseInRect(contextRef, circlePoint);
}
}
Heres' where I am calling it:
...
UIView* vTopWrapper = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-200.0, 50.0, 400.0, 50.0)];
vTopWrapper.layer.borderWidth = 1.0;
float thisX = 0.0;
for (int x=0; x<10; x++) {
FEG_CIRCLE* thisCircle = [[FEG_CIRCLE alloc] initWithFrame:CGRectMake(thisX, 0.0, 25.0, 25.0)];
[vTopWrapper addSubview:thisCircle];
[thisCircle setMyCircle:FEG_RA_ORANGE :20.0];
thisX = thisX + 30.0;
}
DDLogVerbose(@"%@", vTopWrapper.subviews);
[self.view addSubview:vTopWrapper];
...
Here's the console log:
"<FEG_CIRCLE: 0xa09a790; frame = (0 0; 25 25); layer = <CALayer: 0xa09a860>>",
"<FEG_CIRCLE: 0xa09aaf0; frame = (30 0; 25 25); layer = <CALayer: 0xa09ab60>>",
"<FEG_CIRCLE: 0xa09abf0; frame = (60 0; 25 25); layer = <CALayer: 0xa09ac60>>",
"<FEG_CIRCLE: 0xa09acf0; frame = (90 0; 25 25); layer = <CALayer: 0xa09ad60>>",
"<FEG_CIRCLE: 0xa09adf0; frame = (120 0; 25 25); layer = <CALayer: 0xa09ae60>>",
"<FEG_CIRCLE: 0xa09aef0; frame = (150 0; 25 25); layer = <CALayer: 0xa09af60>>",
"<FEG_CIRCLE: 0xa09aff0; frame = (180 0; 25 25); layer = <CALayer: 0xa09b060>>",
"<FEG_CIRCLE: 0xa09b150; frame = (210 0; 25 25); layer = <CALayer: 0xa09aa60>>",
"<FEG_CIRCLE: 0xa09b220; frame = (240 0; 25 25); layer = <CALayer: 0xa09b290>>",
"<FEG_CIRCLE: 0xa09b320; frame = (270 0; 25 25); layer = <CALayer: 0xa09b390>>"
But in my simulator I only see one circle displayed, at the first 30,0 position.
Any help would be appreciated.
Upvotes: 1
Views: 114
Reputation: 57168
When you're implementing -drawRect
, you're only rendering the space inside that particular view. This means that the area you want to draw in is the rectangle with an origin at (0,0) with width and height matching the view's bounds. The system takes care of positioning what you draw in the correct place in the parent view.
So, when you do this in your -drawRect
method:
CGRect circlePoint = (CGRectMake(self.frame.origin.x, self.frame.origin.y, _radius, _radius));
You're actually drawing the circle outside of the view when the frame's origin is not (0,0). You probably want:
CGRect circlePoint = (CGRectMake(0, 0, _radius, _radius));
Upvotes: 1