Drawrect :()By default, nothing is done,
1. If you base a uiview, The subclass can use the core graphics framework and uikit to complete the painting operation in this method.
2. If you use other methods to set the content of the subclass, this method is not applicable.
For example, you only need to change the background color, or use its underlying layer object (including directly using the basic layer of the subclass, or adding the subview to the subclass ).
3. if selfview is inherited directly from a uiview object (including all the uiview subclasses provided by the system), you do not need to call Super when enabling this method, however, if you inherit a custom uiview subclass like myview: selfview, call Super at some point in your implementation if myview needs to use drawrect: method.
4. this method is automatically called when the first displayed view or when an event needs to change the visible view. You cannot directly call this method, you can use setneedsdisplay or setneedsdisplayinrect as an alternative.
5. If you use uiview for plotting, you can only obtain the corresponding contextref in the drawrect: method and plot it. If it is obtained in other methods, an invalidate ref will be obtained and cannot be used for drawing. Drawrect: The call method cannot be displayed manually. You must call setneedsdisplay or setneedsdisplayinrect to enable the system to automatically call this method.
6. If you use calayer to draw a chart, you can only draw it in drawincontext: (similar to drawrect) or the corresponding method in delegate. The preceding method is also called indirectly, such as setneeddisplay.
7. To draw images in real time, you cannot use gesturerecognizer. You can only use methods such as touchbegan to use setneedsdisplay to refresh the screen in real time.
---------- Code:
# Import "infoview. H"
@ Interface uiview (canvasimageview) // Add a category
// Polygon
-(Void) drawpolygon :( nsarray *) pointarray;
@ End
@ Implementation uiview (canvasimageview)
-(Void) drawpolygon :( nsarray *) pointarray
{
// Nsassert (pointarray. Count> = 2, @ "the array length must be greater than or equal to 2 ");
// Nsassert ([[pointarray [0] class] issubclassofclass: [nsvalue class], @ "the array member must be nsvalue composed of cgpoint ");
Cgcontextref context = uigraphicsgetcurrentcontext ();
Nsvalue * startpointvalue = pointarray [0];
Cgpoint startpoint = [startpointvalue cgpointvalue];
Cgcontextmovetopoint (context, startpoint. X, startpoint. y );
For (INT I = 1; I <pointarray. Count; I ++)
{
// Nsassert ([[pointarray [I] class] issubclassofclass: [nsvalue class], @ "the array member must be nsvalue consisting of cgpoint ");
Nsvalue * pointvalue = pointarray [I];
Cgpoint point = [pointvalue cgpointvalue];
Cgcontextaddlinetopoint (context, point. X, point. y );
}
[[Uicolor redcolor] setfill];
[[Uicolor yellowcolor] setstroke];
Cgcontextdrawpath (context, kcgpathfillstroke );
}
@ End
@ Implementation infoview
-(ID) initwithframe :( cgrect) Frame
{
Self = [Super initwithframe: frame];
If (Self ){
// Initialization code
}
Return self;
}
/* Only override drawrect: If you perform custom drawing. An empty implementation adversely affects performance during animation .*/
-(Void) drawrect :( cgrect) rect
{
// Drawing code
Cgpoint uppoint = cgpointmake (rect. Size. width/2, rect. Size. Height/2-15 );
Cgpoint downpoint = cgpointmake (rect. Size. width/2, rect. Size. Height/2 + 15 );
Cgpoint Arrowpoint = cgpointmake (0, rect. Size. Height );
Nsarray * pointarr = [nsarray destination: [nsvalue valuewithcgpoint: uppoint], [nsvalue destination: downpoint], [nsvalue destination: Arrowpoint], [nsvalue destination: uppoint], nil];
// The cgcontext used for this code drawing can only be used in drawrect: The method will be invalid if it is put elsewhere, because the method is called and the view will be re-drawn, and overwrite the previous attempt. You can plot the underlying layer in other places ~
[Self drawpolygon: pointarr];
}
@ End