繪製圖形-移動,旋轉,陰影,漸層

來源:互聯網
上載者:User

標籤:繪圖   對圖形的操作   

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    UIBezierPath *path = [UIBezierPathbezierPath];

    //繪圖的起點座標

    [path moveToPoint:CGPointMake(50,50)];

    //依次串連各個座標

    [path addLineToPoint:CGPointMake(50,100)];

    [path addLineToPoint:CGPointMake(100,100)];

    [path addLineToPoint:CGPointMake(100,50)];

   //閉合

    [pathclosePath];

    //上下文

    CGContextRef context =UIGraphicsGetCurrentContext();

    //儲存上下文狀態

    CGContextSaveGState(context);

    //繪製圖形輪廓

    [pathstroke];

    //移動原點,使各個座標依次移動

    CGContextTranslateCTM(context,50,50);

    //設定顏色

    UIColor *fillColor = [UIColorgreenColor];

    //填充顏色

    [fillColorsetFill];

   //填充

    [pathfill];

    //再次移動原點座標

    CGContextTranslateCTM(context,50,50);

    //圖形旋轉

    CGContextRotateCTM(context,3.14/4);

    fillColor = [UIColororangeColor];

    [pathfill];

    //[path stroke];

    //恢複上下文狀態,

    CGContextRestoreGState(context);

    

    CGContextSaveGState(context);

    CGContextTranslateCTM(context,50,200);

    //圖形陰影

    CGContextSetShadow(context,CGSizeMake(10,10),7);

    fillColor = [UIColorgrayColor];

    [fillColorsetFill];

    [pathfill];

    CGContextRestoreGState(context);//恢複上下文狀態

    

    CGContextSaveGState(context);//儲存上下文狀態

    CGContextTranslateCTM(context,50,300);

    

    //定義漸層

   CGGradientRef myGradient;

    //聲明色彩空間

    CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();

   CGFloat components[8] = {1.0,0.0, 0.0, 1.0,

           0.0, 0.0, 1.0, 1.0

    };

   CGFloat locations[2] = {0.2,0.8};

   int num = 2;

    myGradient =CGGradientCreateWithColorComponents(colorSpace, components, locations, num);

    [pathaddClip];

    CGContextDrawLinearGradient(context, myGradient,CGPointMake(0,0),CGPointMake(100,0),0);

    

    CGColorSpaceRelease(colorSpace);

   CGGradientRelease(myGradient);

    CGContextRestoreGState(context);

   

}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

繪製圖形-移動,旋轉,陰影,漸層

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.