Coregraphics Cgaffinetransform affine transformation (3)

Source: Internet
Author: User

coregraphics cgaffinetransform affine transformation (3)

The affine transformations of the coregraphics can be used for panning, rotating, zooming, or drawing contexts.

(1) The translation transformation pans the current position of the shape in the path or graphic context to another relative position. For example, if you draw a point at (10,20) position, apply a translation transformation to it (30,40), and then draw it, it will be drawn at (40,60) position. To create a translation transformation, using the Cgaffinetransformmaketranslation function, it returns an affine transformation of type Cgaffinetransform, which has two parameters that specify the amount of translation in points in the X and y directions. We can also apply transformations to the graphics context using the CGCONTEXTTRANSLATECTM procedure.

Panning Transform paths

?
12345678910111213141516171819 //平移变换-(void)drawRect:(CGRect)rect{    CGMutablePathRef path = CGPathCreateMutable();    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);        CGAffineTransform transform = CGAffineTransformMakeTranslation(100.0f, 0.0f);    CGContextRef currentContext = UIGraphicsGetCurrentContext();    CGPathAddRect(path, &transform, rectangle);    CGContextAddPath(currentContext, path);    [[UIColor brownColor] setStroke];    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];    CGContextSetLineWidth(currentContext, 5.0f);    CGContextDrawPath(currentContext, kCGPathFillStroke);    CGPathRelease(path);}

Panning Transformation Graphics Context

?
12345678910111213141516171819202122232425 -(void)drawRect:(CGRect)rect{    CGMutablePathRef path = CGPathCreateMutable();    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);    CGPathAddRect(path, NULL, rectangle);        CGContextRef currentContext = UIGraphicsGetCurrentContext();    CGContextSaveGState(currentContext);        CGContextTranslateCTM(currentContext, 100.0f, 40.0f);    CGContextAddPath(currentContext, path);    [[UIColor colorWithRed:0.20f green:0.6f blue:0.8f alpha:1.0f] setFill];    [[UIColor brownColor] setStroke];        CGContextSetLineWidth(currentContext, 5.0f);    CGContextDrawPath(currentContext, kCGPathFillStroke);        CGPathRelease(path);        CGContextRestoreGState(currentContext);            }

(2) Scaling is another transformation that you can use. You can easily get coregraphics to zoom in on a shape, such as a circle that zooms to 100 times times the original. To create an affine scaling transformation, use the Cgaffinetransformmakescale function, which returns a Transform object of type Cgaffinetransform. If you want to use a zoom transform directly on a graphics context, use the CGCONTEXTSCALECTM procedure to scale the current transformation matrix.

Zoom path

?
1234567891011121314151617 -(void)drawRect:(CGRect)rect{    CGMutablePathRef path = CGPathCreateMutable();    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);        CGAffineTransform transform = CGAffineTransformMakeScale(0.5f, 0.5f);    CGContextRef currentContext = UIGraphicsGetCurrentContext();    CGPathAddRect(path, &transform, rectangle);    CGContextAddPath(currentContext, path);    [[UIColor brownColor] setStroke];    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];    CGContextSetLineWidth(currentContext, 5.0f);    CGContextDrawPath(currentContext, kCGPathFillStroke);    CGPathRelease(path);}
Zoom the graphics context?
1234567891011121314151617181920212223242526 -(void)drawRect:(CGRect)rect{    CGMutablePathRef path = CGPathCreateMutable();    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);    CGPathAddRect(path, NULL, rectangle);        CGContextRef currentContext = UIGraphicsGetCurrentContext();    CGContextSaveGState(currentContext);        CGContextScaleCTM(currentContext, 0.5f, 0.5f);    CGContextAddPath(currentContext, path);    [[UIColor colorWithRed:0.20f green:0.6f blue:0.8f alpha:1.0f] setFill];    [[UIColor brownColor] setStroke];        CGContextSetLineWidth(currentContext, 5.0f);    CGContextDrawPath(currentContext, kCGPathFillStroke);        CGPathRelease(path);        CGContextRestoreGState(currentContext);            }

(3) Just like zoom and pan, you can apply a rotation transform to the shape and graphics context that you draw on the path. You can use the Cgaffinetransformmakeroation function and a rotated radian value to get a cgaffinetransform type of transformation. If you want to rotate the specified angle for the entire graphics context, you can use the CGCONTEXTROTATECTM procedure.

Rotation path

?
123456789101112131415 -(void)drawRect:(CGRect)rect{    CGMutablePathRef path = CGPathCreateMutable();    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);        CGAffineTransform transform = CGAffineTransformMakeRotation((45.0f * M_PI) / 180.0f);    CGContextRef currentContext = UIGraphicsGetCurrentContext();    CGPathAddRect(path, &transform, rectangle);    CGContextAddPath(currentContext, path);    [[UIColor brownColor] setStroke];    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];    CGContextSetLineWidth(currentContext, 5.0f);    CGContextDrawPath(currentContext, kCGPathFillStroke);    CGPathRelease(path);}
Rotate the graphics context?
1234567891011121314151617181920212223 -(void)drawRect:(CGRect)rect{    CGMutablePathRef path = CGPathCreateMutable();    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);    CGPathAddRect(path, NULL, rectangle);        CGContextRef currentContext = UIGraphicsGetCurrentContext();    CGContextSaveGState(currentContext);    CGContextRotateCTM(currentContext, (45.0f * M_PI) / 180.0f);    CGContextAddPath(currentContext, path);    [[UIColor colorWithRed:0.20f green:0.6f blue:0.8f alpha:1.0f] setFill];    [[UIColor brownColor] setStroke];        CGContextSetLineWidth(currentContext, 5.0f);    CGContextDrawPath(currentContext, kCGPathFillStroke);        CGPathRelease(path);        CGContextRestoreGState(currentContext);            }
In addition, we can combine the transformation effect, using the Cgaffinetransformconcact function to combine two transform effects, the function of two parameters are type Cgaffinetransform type of transformation.

Combine multiple transform effects while panning and zooming

?
1234567891011121314151617 -(void)drawRect:(CGRect)rect{    CGMutablePathRef path = CGPathCreateMutable();    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);        CGAffineTransform transform1 = CGAffineTransformMakeTranslation(100.0f, 0.0f);    CGAffineTransform transform2 = CGAffineTransformMakeScale(0.5f, 0.5f);    CGAffineTransform transform = CGAffineTransformConcat(transform1, transform2);    CGContextRef currentContext = UIGraphicsGetCurrentContext();    CGPathAddRect(path, &transform, rectangle);    CGContextAddPath(currentContext, path);    [[UIColor brownColor] setStroke];    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];    CGContextSetLineWidth(currentContext, 5.0f);    CGContextDrawPath(currentContext, kCGPathFillStroke);    CGPathRelease(path);}

 

Coregraphics Cgaffinetransform affine transformation (3)

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.