iOS Advanced-quartzcore Framework-graphics context stack, matrix manipulation, cropping, redrawing (brush frames)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.