天天看點

CoreGraphics 之CGAffineTransform仿射變換(3)

 CoreGraphics 之CGAffineTransform仿射變換(3)

 CoreGraphics 的 仿射變換 可以用于 平移、旋轉、縮放變換路徑 或者圖形上下文。 

   (1)平移變換将路徑或圖形上下文中的形狀的目前位置平移到另一個相對位置。舉例來說,如果你在(10,20)的位置處畫一個點,對它應用(30,40)的平移變換,然後繪制它,這個點将被繪制在(40,60)的位置處。為了建立一個平移變換,使用CGAffineTransformMakeTranslation函數,它将傳回一個CGAffineTransform類型的仿射變換,這個函數的兩個參數指定x和y方向上以點為機關的平移量。我們還可以使用CGContextTranslateCTM過程對圖形上下文應用變換。 

 平移變換路徑 

//平移變換
 
-(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);
}      

平移變換圖形上下文 

-(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)縮放是另外一個你可以使用的變換。你可以很容易地讓CoreGraphics 對形狀進行縮放,例如一個圓形縮放到原來的100倍。要建立一個仿射縮放變換,使用CGAffineTransformMakeScale函數,它傳回一個CGAffineTransform 類型的變換對象。如果你想直接對一個圖形上下文使用縮放變換,使用CGContextScaleCTM過程來縮放目前變換矩陣。 

 縮放路徑 

-(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);
}      

  縮放圖形上下文 

-(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)就像縮放和平移,你可以對繪制在路徑上的形狀和圖形上下文應用旋轉變換。你可以使用CGAffineTransformMakeRoation函數和一個旋轉的弧度值來擷取一個CGAffineTransform類型的變換.如果你想對整個圖形上下文旋轉指定角度,可以使用CGContextRotateCTM過程。 

  旋轉路徑 

-(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);
}      

  旋轉圖形上下文 

-(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);
     
     
     
}      

   另外我們還可以組合變換效果,使用 CGAffineTransformConcact函數組合兩個變換效果,這個函數的兩個參數都是類型為CGAffineTransform類型的變換。

-(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);
}