Search code examples
iosobjective-cuiviewcore-graphicscore-animation

Drawing inside a subview of a UIView subclass


I have more of a generic question. I have a custom class that subclasses UIView. I override '-drawRect' to draw points in a line which I successfully did. Now I declare another UIView object inside my class that I want to animate. It should be a white circle outline which I'd like to move with an animation and this is why I want it to be of UIView type. But how can I draw inside it? What if I have 3 other similar cases for example? So my point is, I want to keep all of the functionality inside the component's class and have custom drawing for one or more UIView subviews. I think I am missing something on conceptional level.

In ActionScript 3 I would do it so:

var first:MovieClip = new MovieClip();   
first.graphics...
......
var nTh:MovieClip = new MovieClip();
nTh.graphics...

This is what I want to achieve (as I said I've already made the drawing of the dots inside -drawRect, now I want to make the bigger circle which has to be animated later): example


Solution

  • What I did is creating an UIView instance and setting its bounds and position in -initWithFrame:

    //circleFrameSideSize will serve as the diameter of the circle
    UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(0, circleYPos, circleFrameSideSize, circleFrameSideSize)];
    circle.layer.borderColor = [UIColor whiteColor].CGColor;
    circle.layer.borderWidth = 1;
    //here we do the trick for making the circle with using layer's corner radius
    circle.layer.cornerRadius = circleFrameSideSize/2;
    [self addSubview:circle];