****注意一点是set 和setFill的区别
-(void)drawRect:(CGRect)rect{
UIColor *fillColor = [UIColor orangeColor];
[fillColor setFill];
UIBezierPath *path = [UIBezierPath bezierPath];
//setting the starting point of shape
[path moveToPoint:CGPointMake(100+30, 100+0)];
//draw the lines
[path addLineToPoint:CGPointMake(200+30, 100+40.0)];
[path addLineToPoint:CGPointMake(160.0+30, 100+140.0)];
[path addLineToPoint:CGPointMake(40.0+30, 100+140.0)];
[path addLineToPoint:CGPointMake(0.0+30, 100+40.0)];
[path closePath];
[path fill];
}
-(void)drawRect:(CGRect)rect{
UIColor *fillColor = [UIColor orangeColor];
[fillColor set];
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 5;
path.lineJoinStyle = kCGLineCapRound;
path.lineCapStyle = kCGLineCapRound;
[path moveToPoint:CGPointMake(100, 20)];
[path addLineToPoint:CGPointMake(200+30, 100+40.0)];
[path addLineToPoint:CGPointMake(160.0+30, 100+140.0)];
[path addLineToPoint:CGPointMake(40.0+30, 100+140.0)];
[path addLineToPoint:CGPointMake(0.0+30, 100+40.0)];
[path addLineToPoint:CGPointMake(100, 20)];
[path stroke];
}
周长= 2πradius
define DEGREES_TO_RADIANS(degrees) (M_PI * degrees/ 180)
-(void)drawRect:(CGRect)rect{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100,100) radius:100 startAngle:0 endAngle:DEGREES_TO_RADIANS(135) clockwise:YES];
[path closePath];
[path fill];//填充区域
}
-(void)drawRect:(CGRect)rect{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100,100) radius:100 startAngle:0 endAngle:DEGREES_TO_RADIANS(135) clockwise:YES];
path.lineWidth = 5;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
[path stroke];
}
Adding Curves to Your Path//添加曲线
//由控制点和控制点的切线点决定
-(void)drawRect:(CGRect)rect{
UIColor *fillColor = [UIColor orangeColor];
[fillColor set];
//绘制二次贝赛尔曲线
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 5;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
//setting start point
[path moveToPoint:CGPointMake(20, 100)];
//setting end point control point(中间控制点)
[path addQuadCurveToPoint:CGPointMake(120, 100) controlPoint:CGPointMake(70, 300)];
[path stroke];
}
-(void)drawRect:(CGRect)rect{
UIColor *fillColor = [UIColor orangeColor];
[fillColor set];
//绘制二次贝赛尔曲线
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 5;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
//setting start point
[path moveToPoint:CGPointMake(20, 100)];
//setting end point control point(中间控制点)
[path addQuadCurveToPoint:CGPointMake(120, 100) controlPoint:CGPointMake(70, 300)];
[path stroke];
[path fill];
}
-(void)drawRect:(CGRect)rect{
UIColor *color = [UIColor orangeColor];
[color set];
//绘制3次贝赛尔曲线
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 5;
path.lineJoinStyle = kCGLineCapRound;
path.lineCapStyle = kCGLineCapRound;
[path moveToPoint:CGPointMake(20, 50)];
[path addCurveToPoint:CGPointMake(300, 50) controlPoint1:CGPointMake(100, 0) controlPoint2:CGPointMake(200, 400)];
[path stroke];
}
-(void)drawRect:(CGRect)rect{
UIColor *color = [UIColor orangeColor];
[color set];
//绘制3次贝赛尔曲线
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 5;
path.lineJoinStyle = kCGLineCapRound;
path.lineCapStyle = kCGLineCapRound;
[path moveToPoint:CGPointMake(20, 50)];
[path addCurveToPoint:CGPointMake(300, 50) controlPoint1:CGPointMake(130, 0) controlPoint2:CGPointMake(200, 400)];
[path stroke];
[path fill];
}
Modifying the Path Using Core Graphics Functions-->使用core graphic 来修改
-(void)drawRect:(CGRect)rect{
//根据坐标画出一个内切圆
UIBezierPath *PATH = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 200)];
[[UIColor blackColor]setStroke];
[[UIColor orangeColor] setFill];
CGContextRef ref = UIGraphicsGetCurrentContext();
// 在视图中的坐标
CGContextTranslateCTM(ref,100, 100);
PATH.lineWidth = 5;
[PATH fill];
[PATH stroke];
}
//根据一个矩形画曲线
- (UIBezierPath *)bezierPathWithRect:(CGRect)rect
//根据矩形框的内切圆画曲线
- (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect
//根据矩形画带圆角的曲线
- (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
//在矩形中,可以针对四角中的某个角加圆角
-
(UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
UIBezierPath *PATH = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 200)byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(40, 10)];
-----------CoreGraphics--------------------
-(void)drawRect:(CGRect)rect{
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectOne = CGRectMake(10, 10, 200, 300);
CGRect rectTwo = CGRectMake(40, 100, 90, 300);
CGRect rects[2] = {rectOne,rectTwo};
// CGPathAddRect(path, NULL, rectOne); 一个矩形 下面是两个矩形的情况
CGPathAddRects(path, NULL, (const CGRect *)&rects, 2);
CGContextRef current = UIGraphicsGetCurrentContext();
CGContextAddPath(current, path);
[[UIColor orangeColor]setFill];
[[UIColor purpleColor]setStroke];
CGContextSetLineWidth(current, 5);
CGContextDrawPath(current, kCGPathFillStroke);
CGPathRelease(path);
}
-(void)drawRect:(CGRect)rect{
CGContextRef currentRef = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor(currentRef, CGSizeMake(10, 10), 20.f, [UIColor redColor].CGColor);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(55, 60, 150, 150));
CGContextAddPath(currentRef, path);
[[UIColor orangeColor]setFill];
CGContextDrawPath(currentRef, kCGPathFill);
CGPathRelease(path);
}
-(void)drawAtTop{
CGContextRef currentRef = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor(currentRef, CGSizeMake(10, 10), 20.f, [UIColor redColor].CGColor);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(55, 60, 150, 150));
CGContextAddPath(currentRef, path);
[[UIColor orangeColor]setFill];
CGContextDrawPath(currentRef, kCGPathFill);
CGPathRelease(path);
}
-(void)drawAtBottom{
//不特意加阴影也会出现阴影效果
CGContextRef current = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(150, 250, 100, 100));
CGContextAddPath(current, path);
[[UIColor purpleColor]setFill];
CGContextDrawPath(current, kCGPathFill);
CGPathRelease(path);
}
由于上述 原因 所以我们可以使用CGContextSaveGState来保存图形上下文状态,可以通过CGContextRestoreGState来恢复到以前的状态。
-(void)drawAtTop{
CGContextRef currentRef = UIGraphicsGetCurrentContext();
//保存状态
CGContextSaveGState(currentRef);
CGContextSetShadowWithColor(currentRef, CGSizeMake(10, 10), 20.f, [UIColor redColor].CGColor);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(55, 60, 150, 150));
CGContextAddPath(currentRef, path);
[[UIColor orangeColor]setFill];
CGContextDrawPath(currentRef, kCGPathFill);
CGPathRelease(path);
//恢复到以前的状态
CGContextRestoreGState(currentRef);
}
-(void)drawAtBottom{
//由于上述方法已经添加保存和修复状态所以这个就没有阴影效果
CGContextRef current = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(150, 250, 100, 100));
CGContextAddPath(current, path);
[[UIColor purpleColor]setFill];
CGContextDrawPath(current, kCGPathFill);
CGPathRelease(path);
}
创建和渐变
-(void)drawRect:(CGRect)rect{
CGContextRef currentRef = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentRef);
//色彩空间对象
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *startColor = [UIColor orangeColor];
UIColor *endColor = [UIColor purpleColor];
//CGColorGetComponents 返回一个包含各元素的数组
CGFloat *startColorComponents = (CGFloat *)CGColorGetComponents(startColor.CGColor);
CGFloat *endColorComponents = (CGFloat *)CGColorGetComponents(endColor.CGColor);
//元素数组
CGFloat colorComponents[8] = {
startColorComponents[0],
startColorComponents[1],
startColorComponents[2],
startColorComponents[3],
endColorComponents[0],
endColorComponents[1],
endColorComponents[2],
endColorComponents[3]
};
CGFloat colorIndices[2] = {0.0f,1.0f};
//返回CGGradientRef 一个对象 gradient(梯度)
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,(const CGFloat *)&colorComponents, (const CGFloat *)&colorIndices, 2);
//改变渐变的图形只需要改变开始和结束的坐标点
CGPoint startPoint = CGPointMake(0, [[UIScreen mainScreen] bounds].size.height/2);
CGPoint endPoint = CGPointMake([[UIScreen mainScreen] bounds].size.width,startPoint.y);
//画线 一个context 梯度 开始结束坐标,
CGContextDrawLinearGradient(currentRef, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation|kCGGradientDrawsAfterEndLocation);
//释放渐变对象
CGGradientRelease(gradient);
CGContextRestoreGState(currentRef);
}
平移图形
-(void)drawRect:(CGRect)rect{
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectAngle = CGRectMake(10, 10, 200, 200);
CGAffineTransform transform = CGAffineTransformMakeTranslation(100, 0);
//主要是传入一个transform 从10 10 到100 0
CGPathAddRect(path, &transform, rectAngle);
CGContextRef currentRef = UIGraphicsGetCurrentContext();
CGContextAddPath(currentRef, path);
[[UIColor orangeColor]setFill];
[[UIColor purpleColor]setStroke];
CGContextSetLineWidth(currentRef, 5);
CGContextDrawPath(currentRef, kCGPathFillStroke);
CGPathRelease(path);
}
CGContextTranslateCTM 利用这个方法一样可以达到上次的效果
-(void)drawRect:(CGRect)rect{
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectAngle = CGRectMake(10, 10, 200, 200);
CGPathAddRect(path, NULL, rectAngle);
CGContextRef currentRef = UIGraphicsGetCurrentContext();
//CGContextTranslateCTM 利用这个方法一样可以达到上次的效果
CGContextTranslateCTM(currentRef, 100, 0);
CGContextAddPath(currentRef, path);
[[UIColor orangeColor]setFill];
[[UIColor purpleColor]setStroke];
CGContextSetLineWidth(currentRef, 5);
CGContextDrawPath(currentRef, kCGPathFillStroke);
CGPathRelease(path);
}
缩放 由200 到100
-(void)drawRect:(CGRect)rect{
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectAngle = CGRectMake(100, 100, 200, 200);
//缩放
CGAffineTransform transform = CGAffineTransformMakeScale(0.5, 0.5);
CGPathAddRect(path, &transform, rectAngle);
CGContextRef currentRef = UIGraphicsGetCurrentContext();
// //CGContextTranslateCTM 利用这个方法一样可以达到上次的效果
// CGContextTranslateCTM(currentRef, 100, 0);
CGContextAddPath(currentRef, path);
[[UIColor orangeColor]setFill];
[[UIColor purpleColor]setStroke];
CGContextSetLineWidth(currentRef, 5);
CGContextDrawPath(currentRef, kCGPathFillStroke);
CGPathRelease(path);
}
-(void)drawRect:(CGRect)rect{
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectAngle = CGRectMake(100, 100, 200, 200);
//缩放
CGAffineTransform transform = CGAffineTransformMakeScale(0.5, 0.5);
CGPathAddRect(path, &transform, rectAngle);
CGContextRef currentRef = UIGraphicsGetCurrentContext();
// //CGContextTranslateCTM 利用这个方法一样可以达到上次的效果
// CGContextTranslateCTM(currentRef, 100, 0);
CGContextAddPath(currentRef, path);
[[UIColor orangeColor]setFill];
[[UIColor purpleColor]setStroke];
CGContextSetLineWidth(currentRef, 5);
CGContextDrawPath(currentRef, kCGPathFillStroke);
CGPathRelease(path);
}
旋转
![Uploading 屏幕快照 2016-01-19 上午11.47.36_529253.png . . .]
-(void)drawRect:(CGRect)rect{
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectAngle = CGRectMake(100, 100, 200, 200);
CGAffineTransform transform = CGAffineTransformMakeRotation((45*M_PI)/180.0f);
CGPathAddRect(path, &transform, rectAngle);
CGContextRef currentRef = UIGraphicsGetCurrentContext();
//CGContextTranslateCTM 利用这个方法一样可以达到上次的效果
CGContextScaleCTM(currentRef, 0.5, 0.5);
CGContextAddPath(currentRef, path);
[[UIColor orangeColor]setFill];
[[UIColor purpleColor]setStroke];
CGContextSetLineWidth(currentRef, 5);
CGContextDrawPath(currentRef, kCGPathFillStroke);
CGPathRelease(path);
}
一样可以实现
CGContextRotateCTM(currentRef, 30*M_PI / 180.f);
动画和移动视图
[UIView beginAnimations:@"ImageViewAnimation" context:(__bridge void * _Nullable)(_imageView)];
[UIView setAnimationDuration:5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(imageViewStop:finished:context:)];
_imageView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width -100, [[UIScreen mainScreen] bounds].size.height - 100, 100, 100);
[UIView commitAnimations];
-(void)imageViewStop:(NSString *)paramAnimationID finished:(NSNumber *)paramFinished context:(void *)paramContext{
NSLog(@"animation finished");
NSLog(@"animations__ID___%@",paramAnimationID);
UIImageView *contextImageView = (__bridge UIImageView *)paramContext;
NSLog(@"imageView————%@",contextImageView);
}
输出
**2016-01-19 16:17:58.881 LoginTest[5362:1296744] animation finished**
**2016-01-19 16:17:58.881 LoginTest[5362:1296744] animations__ID___ImageViewAnimation**
**2016-01-19 16:17:58.882 LoginTest[5362:1296744] imageView————<UIImageView: 0x7fa2134a82c0; frame = (314 636; 100 100); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7fa21371b000>>**
缩放
_imageView.center = self.view.center;
_imageView.transform = CGAffineTransformIdentity;
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5];
_imageView.transform = CGAffineTransformMakeScale(0.5, 0.5);
[UIView commitAnimations];
旋转
_imageView.center = self.view.center;
_imageView.transform = CGAffineTransformIdentity;
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:2];
_imageView.transform = CGAffineTransformMakeRotation((45*M_PI)/180);
[UIView commitAnimations];