You create your DrawCircle, but you never add it to the view, for example.
[self.view addSubview:circleView]
Therefore, it goes out of scope and (if using ARC) is freed from you. You also do not set its frame , for example:
circleView = [[DrawCircle alloc] initWithFrame:self.view.bounds];
Alternatively, you can add a view to the interface constructor (standard UIView , but then specify your own class in IB).
Also, note that you usually donβt even call setNeedsDisplay . Adding this to the view hierarchy will cause this for you. You only need to call this if you need to update the view based on some custom property.
Personally, I would be inclined to define drawRect as follows:
- (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext();
This way, the location and size of the circle will be dictated by the frame set for the circle (by the way, I apologize if this makes it more confusing, but I use a different class name, CircleView , because I like View in the name of my UIView subclasses).
- (void)viewDidLoad { [super viewDidLoad]; UIView *circle; circle = [[CircleView alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 200.0)]; circle.backgroundColor = [UIColor clearColor];
source share