About drawing using quartz 2D on iPhone
I have a 开发者_StackOverflowview, that have a drawRect method, I know that this method is the only way I control the View to draw something on it. So, I try to my drawing logic in this way:
- (void)drawRect:(CGRect)rect {
//my drawing code...
}
In my view, I use the IB to link this class.
[myView setNeedsDisplay];
It works, so, I designed to have a Command object inside the drawRect method, so that I can drawing dynamically based on my Cmd. Here is the code in the View after I modified:
- (void)drawRect:(CGRect)rect {
self.cmdToBeExecuted = [[DrawingSomethingCmd alloc] init];
[self.cmdToBeExecuted execute];
}
My DrawingsomthingCmd:
@implementation DrawingSomethingCmd
-(void)execute{
//my drawing code;
}
It works too. But the question is, how can I assign the self.cmdToBeExecuted dynamically. Also, I changed my drawRect like this:
- (void)drawRect:(CGRect)rect {
[self.cmdToBeExecuted execute];
}
Because I have this to link with the IB,
IBOutlet myDrawingView *myView;
but after I type [myView ... ...], it don't allow me to get the variable cmdToBeExecuted. I ready make my variable accessible in .h:
@property (nonatomic, retain) Command *cmdToBeExecuted;
and .m also:
@synthesize cmdToBeExecuted;
Don't initialize the command inside the drawing rect. Initialize a default (maybe in viewDidLoad? it depends on what you are doing) somewhere as the view is created and then update them dynamically however you are doing this, whenever the need arises. So:
- (void)drawRect:(CGRect)rect
{
[[self commandToBeExecuted] execute];
}
and elsewhere:
// dynamically update the drawing
[myView setCommandToBeExected:[[[DrawingSomethingCommand alloc] init] autorelease]];
精彩评论