Basic points:
1, paint can not be in the Viewcontroller, but should be in a UIView subclass, such as a new drawview inherit from UIView.
2, covering UIView's DrawRect method, makes it to draw the graph that conforms to the need.
#import<UIKit/UIKit.h>@interfaceDrawview:uiview@end/*****************************/#import "DrawView.h"@implementationDrawviewStatic intHeight =0;- (void) DrawRect: (cgrect) rect{NSLog (@"DrawRect"); Height= height +Ten; CGRect bounds=[self bounds]; [[Uicolor Yellowcolor]Set]; Uirectfill (bounds); CGRect Square= CGRectMake ( -, -, -, height); [[Uicolor Greencolor]Set]; Uirectfill (square); [[Uicolor Blackcolor]Set]; Uirectframe (square);}@end
3, drawing is automatically called by an object call to Drawview ([view setneedsdisplay];) to draw drawrect. View needs to be a Drawview object.
-(Ibaction) Drawview: (ID) sender{ NSLog (@ "drawview"); [View Setneedsdisplay];}
4, note that there are several situations where drawrect is not called:
(1) When the size of the view frame is (0,0), the system does not execute the DrawRect method.
(2) When this view is not Superview (view is the outermost UIView), it will still not execute DrawRect
Additional note: When View.hidden=yes or view.frame out of bounds, the DrawRect method is still called, which shows that there are two cases where the DrawRect method is not executed.
IOS Drawing Basics