First, the graphics context stack
1. Customize a Mjview, inherit from UIView
2. Set the default view's class to Mjview
3. Implementing DrawRect: Methods
-(void) DrawRect: (cgrect) rect
{
1. Get context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Put the current CTX copy on the stack
Cgcontextsavegstate (CTX);
Set drawing status
Cgcontextsetlinewidth (ctx,10);
[[Uicolor Redcolor] set];
Cgcontextsetlinecap (Ctx,kcglinecapround);
1th Line
Cgcontextmovetopoint (ctx,50,,50);
Cgcontextaddlinetopoint (ctx,120,190);
Cgcontextstrokepath (CTX);
Stacks the context of the top of the stack, replacing the current context (so the state goes back to the original)
Cgcontextrestoregstate (CTX);
2nd line
Cgcontextmovetopoint (ctx,200,,70);
Cgcontextaddlinetopoint (ctx,220,290);
Cgcontextstrokepath (CTX);
}
Second, matrix operation
1. Customize a Mjview, inherit from UIView
2. Drag a view to set the class to Mjview
3. Implementing DrawRect: Methods
-(void) DrawRect: (cgrect) rect
{
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Matrix operations
CGCONTEXTROTATECTM (Ctx,m_pi_4);
CGCONTEXTSCALECTM (ctx,0.5,,0.5);
CGCONTEXTTRANSLATECTM (ctx,0,50);
Cgcontextaddrect (Ctx,cgrectmake (10,10,50,50));
Cgcontextaddellipseinrect (Ctx,cgrectmake (100,100,100,100));
Cgcontextmovetopoint (ctx,100,100);
Cgcontextaddlinetopoint (ctx,200,200);
Cgcontextstrokepath (CTX);
}
Three, cutting
Cropping principle: First determine the size that needs to be cropped, then put the picture on it, and the parts beyond the range are not displayed
1. Customize a Mjclipview, inherit from UIView
2. Set the default view's class to Mjclipview
3. Implementing DrawRect: Methods
-(void) DrawRect: (cgrect) rect
{
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
1. Draw a Circle
Cgcontextaddellipseinrect (Ctx,cgrectmake (100,100,50,50));
2. Cropping
Cgcontextclip (CTX);
Cgcontextfillpath (CTX);
3. Show pictures
UIImage *image = [UIImage imagenamed:@ "Me"];
[Image Drawatpoint:cgpointmake (100,100)];
)
Four, redraw (brush frame)
1. Customize a Mjview, inherit from UIView, add a property to the radius of the circle
2. Drag a view to set the class to Mjview
3. Listen to view (Outlet), then drag a slider and listen (Action)
4. Implementing DrawRect: Methods
The default is only called when the view is first displayed (it can only be called automatically by the system and cannot be called manually)
-(void) DrawRect: (cgrect) rect
{
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Cgcontextaddarc (CTX,125,125,SELF.RADIUS,0,M_PI * 2,0);
Cgcontextfillpath (CTX);
}
5. Rewrite the Set method in MJVIEW.M (so you can draw directly with point syntax)
-(void) Setradius: (float) Radius
{
_radius = radius;
Redraw (This method internally calls the DrawRect: method to draw)
[Self setneedsdisplay];
}
5. How to implement the slider bar
-(Ibaction) Sizechange: (UISlider *) sender{
Self.cirecleView.radius = Sender.value;
}
iOS Advanced-quartzcore Framework-graphics context stack, matrix manipulation, cropping, redrawing (brush frames)