首页 > 安全资讯 >

绘制图形-移动,旋转,阴影,渐变

15-07-02

Only override drawRect: if you perform custom drawing An empty implementation adversely affects performance during animation - (void)drawRect:(CGRect)rect { UIBezierPath

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

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

UIBezierPath *path = [UIBezierPath bezierPath];

//绘图的起点坐标

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

//依次连接各个坐标

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

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

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

//闭合

[path closePath];

//上下文

CGContextRef context = UIGraphicsGetCurrentContext();

//保存上下文状态

CGContextSaveGState(context);

//绘制图形轮廓

[path stroke];

//移动原点,使各个坐标依次移动

CGContextTranslateCTM(context, 50, 50);

//设置颜色

UIColor *fillColor = [UIColor greenColor];

//填充颜色

[fillColor setFill];

//填充

[path fill];

//再次移动原点坐标

CGContextTranslateCTM(context, 50, 50);

//图形旋转

CGContextRotateCTM(context, 3.14/4);

fillColor = [UIColor orangeColor];

[path fill];

//[path stroke];

//恢复上下文状态,

CGContextRestoreGState(context);

 

CGContextSaveGState(context);

CGContextTranslateCTM(context, 50, 200);

//图形阴影

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

fillColor = [UIColor grayColor];

[fillColor setFill];

[path fill];

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

[path addClip];

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

 

CGColorSpaceRelease(colorSpace);

CGGradientRelease(myGradient);

CGContextRestoreGState(context);

 

}


 

相关文章
最新文章
热点推荐