Reputation: 29
I am trying to draw a circle after clicking a button. I can create a rectangle but cannot draw a circle.
Is there a way to make this:
-(IBAction) buttonTouched
{
}
call or signal this to work:
- (void)drawRect:(CGRect)rect
{
}
Thanks for any input!
*Edit** Sorry, it's not working. Not sure what I'm doing wrong:
1) Created a new view-based project named "test". 2) Created a Objective-C class UIView named "view". 3) In IB dragged a button onto the view, linked it to "buttonTouched" - Touched Up Inside.
testViewController.h
#import <UIKit/UIKit.h>
#import "view.h"
@interface testViewController : UIViewController {
}
-(IBAction) buttonTouched;
@end
testViewController.m
#import "testViewController.h"
#import "view.h"
@implementation testViewController
- (IBAction)buttonTouched {
[[self view] setNeedsDisplay];
}
view.m
#import "view.h"
@implementation view
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25));
CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.0);
CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25));
CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.0);
}
- (void)dealloc {
[super dealloc];
}
@end
Upvotes: 1
Views: 12026
Reputation: 3356
This should do what you want it to. Check Sample code
// The color is by this line CGContextSetRGBFillColor( context , red , green , blue , alpha);
// Draw a circle (filled)
CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25));
CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.0);
// Draw a circle (border only)
CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25));
CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.0);
Upvotes: 6
Reputation: 6427
-(void)drawRect:(CGRect)rect {
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 0, 0, 255, 0.1);
CGContextSetRGBStrokeColor(contextRef, 0, 0, 255, 0.5);
// Draw a circle (filled)
CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25));
// Draw a circle (border only)
CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25));
// Get the graphics context and clear it
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
// Draw a green solid circle
CGContextSetRGBFillColor(ctx, 0, 255, 0, 1);
CGContextFillEllipseInRect(ctx, CGRectMake(100, 100, 25, 25));
// Draw a yellow hollow rectangle
CGContextSetRGBStrokeColor(ctx, 255, 255, 0, 1);
CGContextStrokeRect(ctx, CGRectMake(195, 195, 60, 60));
// Draw a purple triangle with using lines
CGContextSetRGBStrokeColor(ctx, 255, 0, 255, 1);
CGPoint points[6] = {
CGPointMake(100, 200), CGPointMake(150, 250),
CGPointMake(150, 250), CGPointMake(50, 250),
CGPointMake(50, 250), CGPointMake(100, 200)
};
CGContextStrokeLineSegments(ctx, points, 6);
}
Upvotes: 0
Reputation: 385870
If you want the system to call drawRect:
, you call setNeedsDisplay
:
// In your view controller...
- (IBAction)buttonTouched {
[self.view setNeedsDisplay];
}
// In your UIView subclass...
- (void)drawRect:(CGRect)rect {
[UIColor.redColor setFill];
[[UIBezierPath bezierPathWithOvalInRect:self.bounds] fill];
}
Upvotes: 1