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
This should do what you want it to. Check Sample code