self.rectView.angle = (float)self.nowSize/(float)self.fileSize*M_PI*2;
[self.rectView setNeedsDisplay];
-(void)drawTextInRect:(CGRect)rect{
[super drawTextInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint startPoint = CGPointMake(0, self.bounds.size.height/2);
CGPoint endPoint = CGPointMake(self.bounds.size.width, self.bounds.size.height/2);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGContextSetLineWidth(context, 1.0);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
}
#import "CustomView.h"
@implementation CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor (context, 1, 0, 0, 1.0);
UIFont *font = [UIFont boldSystemFontOfSize:15.0];
[@"画圆:" drawInRect:CGRectMake(10, 20, 80, 20) withFont:font];
[@"画线及孤线:" drawInRect:CGRectMake(10, 80, 100, 20) withFont:font];
[@"画矩形:" drawInRect:CGRectMake(10, 120, 80, 20) withFont:font];
[@"画扇形和椭圆:" drawInRect:CGRectMake(10, 160, 110, 20) withFont:font];
[@"画三角形:" drawInRect:CGRectMake(10, 220, 80, 20) withFont:font];
[@"画圆角矩形:" drawInRect:CGRectMake(10, 260, 100, 20) withFont:font];
[@"画贝塞尔曲线:" drawInRect:CGRectMake(10, 300, 100, 20) withFont:font];
[@"图片:" drawInRect:CGRectMake(10, 340, 80, 20) withFont:font];
CGContextSetRGBStrokeColor(context,1,1,1,1.0);
CGContextSetLineWidth(context, 1.0);
CGContextAddArc(context, 100, 20, 15, 0, 2*PI, 0);
CGContextDrawPath(context, kCGPathStroke);
CGContextAddArc(context, 150, 30, 30, 0, 2*PI, 0);
CGContextDrawPath(context, kCGPathFill);
UIColor*aColor = [UIColor colorWithRed:1 green:0.0 blue:0 alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextSetLineWidth(context, 3.0);
CGContextAddArc(context, 250, 40, 40, 0, 2*PI, 0);
CGContextDrawPath(context, kCGPathFillStroke);
CGPoint aPoints[2];
aPoints[0] =CGPointMake(100, 80);
aPoints[1] =CGPointMake(130, 80);
CGContextAddLines(context, aPoints, 2);
CGContextDrawPath(context, kCGPathStroke);
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
CGContextMoveToPoint(context, 140, 80);
CGContextAddArcToPoint(context, 148, 68, 156, 80, 10);
CGContextStrokePath(context);
CGContextMoveToPoint(context, 160, 80);
CGContextAddArcToPoint(context, 168, 68, 176, 80, 10);
CGContextStrokePath(context);
CGContextMoveToPoint(context, 150, 90);
CGContextAddArcToPoint(context, 158, 102, 166, 90, 10);
CGContextStrokePath(context);
CGContextStrokeRect(context,CGRectMake(100, 120, 10, 10));
CGContextFillRect(context,CGRectMake(120, 120, 10, 10));
CGContextSetLineWidth(context, 2.0);
aColor = [UIColor blueColor];
CGContextSetFillColorWithColor(context, aColor.CGColor);
aColor = [UIColor yellowColor];
CGContextSetStrokeColorWithColor(context, aColor.CGColor);
CGContextAddRect(context,CGRectMake(140, 120, 60, 30));
CGContextDrawPath(context, kCGPathFillStroke);
CAGradientLayer *gradient1 = [CAGradientLayer layer];
gradient1.frame = CGRectMake(240, 120, 60, 30);
gradient1.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,
(id)[UIColor grayColor].CGColor,
(id)[UIColor blackColor].CGColor,
(id)[UIColor yellowColor].CGColor,
(id)[UIColor blueColor].CGColor,
(id)[UIColor redColor].CGColor,
(id)[UIColor greenColor].CGColor,
(id)[UIColor orangeColor].CGColor,
(id)[UIColor brownColor].CGColor,nil];
[self.layer insertSublayer:gradient1 atIndex:0];
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
{
1,1,1, 1.00,
1,1,0, 1.00,
1,0,0, 1.00,
1,0,1, 1.00,
0,1,1, 1.00,
0,1,0, 1.00,
0,0,1, 1.00,
0,0,0, 1.00,
};
CGGradientRef gradient = CGGradientCreateWithColorComponents
(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
CGColorSpaceRelease(rgb);
CGContextSaveGState(context);
CGContextMoveToPoint(context, 220, 90);
CGContextAddLineToPoint(context, 240, 90);
CGContextAddLineToPoint(context, 240, 110);
CGContextAddLineToPoint(context, 220, 110);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient,CGPointMake
(220,90) ,CGPointMake(240,110),
kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextMoveToPoint(context, 260, 90);
CGContextAddLineToPoint(context, 280, 90);
CGContextAddLineToPoint(context, 280, 100);
CGContextAddLineToPoint(context, 260, 100);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient,CGPointMake
(260, 90) ,CGPointMake(260, 100),
kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);
CGContextDrawRadialGradient(context, gradient, CGPointMake(300, 100), 0.0, CGPointMake(300, 100), 10, kCGGradientDrawsBeforeStartLocation);
aColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextMoveToPoint(context, 160, 180);
CGContextAddArc(context, 160, 180, 30, -60 * PI / 180, -120 * PI / 180, 1);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextAddEllipseInRect(context, CGRectMake(160, 180, 20, 8));
CGContextDrawPath(context, kCGPathFillStroke);
CGPoint sPoints[3];
sPoints[0] =CGPointMake(100, 220);
sPoints[1] =CGPointMake(130, 220);
sPoints[2] =CGPointMake(130, 160);
CGContextAddLines(context, sPoints, 3);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
float fw = 180;
float fh = 280;
CGContextMoveToPoint(context, fw, fh-20);
CGContextAddArcToPoint(context, fw, fh, fw-20, fh, 10);
CGContextAddArcToPoint(context, 120, fh, 120, fh-20, 10);
CGContextAddArcToPoint(context, 120, 250, fw-20, 250, 10);
CGContextAddArcToPoint(context, fw, 250, fw, fh-20, 10);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextMoveToPoint(context, 120, 300);
CGContextAddQuadCurveToPoint(context,190, 310, 120, 390);
CGContextStrokePath(context);
CGContextMoveToPoint(context, 200, 300);
CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300);
CGContextStrokePath(context);
UIImage *image = [UIImage imageNamed:@"apple.jpg"];
[image drawInRect:CGRectMake(60, 340, 20, 20)];
CGContextDrawImage(context, CGRectMake(100, 340, 20, 20), image.CGImage);
}
@end
demo 图片透明渐变
self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
self.imageView.image = [UIImage imageNamed:@"bg_05.jpg"];
[self addSubview:self.imageView];
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.bounds;
gradientLayer.colors = @[(__bridge id)[UIColor blackColor].CGColor,
(__bridge id)[UIColor clearColor].CGColor,
(__bridge id)[UIColor blackColor].CGColor];
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(0, 1);
[self.layer addSublayer:gradientLayer];
UIView *containerView = [[UIView alloc] initWithFrame:self.imageView.bounds];
[containerView.layer addSublayer:gradientLayer];
self.imageView.maskView = containerView;