****注意一點是set 和setFill的區(qū)別
-(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];//填充區(qū)域
}
-(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{
//不特意加陰影也會出現(xiàn)陰影效果
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來保存圖形上下文狀態(tài),可以通過CGContextRestoreGState來恢復到以前的狀態(tài)黍氮。
-(void)drawAtTop{
CGContextRef currentRef = UIGraphicsGetCurrentContext();
//保存狀態(tài)
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);
//恢復到以前的狀態(tài)
CGContextRestoreGState(currentRef);
}
-(void)drawAtBottom{
//由于上述方法已經添加保存和修復狀態(tài)所以這個就沒有陰影效果
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);
}
創(chuàng)建和漸變
-(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);
}
一樣可以實現(xiàn)
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];