关于贝塞尔曲线画特殊线条的总结

//

//  AppDelegate.h

//  3.01-01-BezierPathandCAShapeLayer

//

//  Created by lyc on 16/3/1.

//  Copyright © 2016 zxh. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface AppDelegate :UIResponder <UIApplicationDelegate>


@property (strong,nonatomic) UIWindow *window;



@end



//

//  AppDelegate.m

//  3.01-01-BezierPathandCAShapeLayer

//

//  Created by lyc on 16/3/1.

//  Copyright © 2016 zxh. All rights reserved.

//


#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end





//

//  ViewController.h

//  3.01-01-BezierPathandCAShapeLayer

//

//  Created by lyc on 16/3/1.

//  Copyright © 2016 zxh. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController



@end





//

//  ViewController.m

//  3.01-01-BezierPathandCAShapeLayer

//

//  Created by lyc on 16/3/1.

//  Copyright © 2016 zxh. All rights reserved.

//


#import "ViewController.h"

#import "MyView.h"


#define UMColor(R,G,B) [UIColor colorWithRed:R/255.f green:G/255.f blue:B/255.f alpha:1.]

#define UMWidth [UIScreen mainScreen].bounds.size.width

#define UMHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

{

    MyView *_myView;

}

@end


@implementation ViewController


- (void)loadView{

    

    _myView = [[MyViewalloc] init];

    self.view =_myView;

}


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

    

//    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(UMWidth/2-0.7, UMHeight/2.0, 0.9, UMHeight/4.0)];

//    line.backgroundColor = UMColor(220, 220, 220);

//    [self.view addSubview:line];


}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end






//

//  MyView.h

//  3.01-01-BezierPathandCAShapeLayer

//

//  Created by lyc on 16/3/1.

//  Copyright © 2016 zxh. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface MyView : UIView


@end






//

//  MyView.m

//  3.01-01-BezierPathandCAShapeLayer

//

//  Created by lyc on 16/3/1.

//  Copyright © 2016 zxh. All rights reserved.

//


#import "MyView.h"


#define UMColor(R,G,B) [UIColor colorWithRed:R/255.f green:G/255.f blue:B/255.f alpha:1.]

#define UMWidth [UIScreen mainScreen].bounds.size.width

#define UMHeight [UIScreen mainScreen].bounds.size.height

#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)


#define PI 3.14159265358979323846

#define radius 100


static inline float radians(double degrees) {

    return degrees * PI / 180;

}


static inlinevoid drawArc(CGContextRef ctx,CGPoint point, float angle_start,float angle_end, UIColor* color) {

    CGContextMoveToPoint(ctx, point.x, point.y);

    CGContextSetFillColor(ctx,CGColorGetComponents( [color CGColor]));

    CGContextAddArc(ctx, point.x, point.y,radius,  angle_start, angle_end, 0);

    //CGContextClosePath(ctx);

    CGContextFillPath(ctx);

}

@implementation MyView


-(void)drawRect:(CGRect)rect

{

//    UIColor *color = UMColor(220, 220, 220);

//    [color set];

//    UIBezierPath *path = [UIBezierPath bezierPath];

//    path.lineWidth = 0.9;

//    path.lineCapStyle = kCGLineCapRound;   //线条拐点

//    path.lineJoinStyle = kCGLineCapRound;  //终点处理

//    [path moveToPoint:CGPointMake(0, UMHeight - 49)];

//    [path addLineToPoint:CGPointMake(UMWidth/2, UMHeight*3/4.0)];

//    [path addLineToPoint:CGPointMake(UMWidth, UMHeight - 49)];

    [path closePath];

//    [path stroke];

    

    //五边形

//    UIColor *color = UMColor(140, 150, 100);

//    [color set];

//    UIBezierPath *path = [UIBezierPath bezierPath];

//    path.lineWidth = 2;

//    path.lineCapStyle = kCGLineCapRound;

//    path.lineJoinStyle = kCGLineCapRound;

//    [path moveToPoint:CGPointMake(100, 20)];

//    [path addLineToPoint:CGPointMake(150, 50)];

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

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

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

//    [path closePath];

//    [path stroke];

    

    //矩形

//    UIColor *color = UMColor(200, 30, 100);

//    [color set];

//    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];

//    path.lineWidth = 4;

//    path.lineCapStyle = kCGLineCapRound;

//    path.lineJoinStyle = kCGLineCapRound;

//    [path stroke];

    

    //圆弧圆形 椭圆

//    UIColor *color = UMColor(180, 40, 70);

//    [color set];

//    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 300) radius:100 startAngle:0 endAngle:DEGREES_TO_RADIANS(270) clockwise:YES];

//    path.lineWidth = 4;

//    path.lineCapStyle = kCGLineCapRound;

//    path.lineJoinStyle = kCGLineCapRound;

//    [path fillWithBlendMode:kCGBlendModeNormal alpha:0.6];

//    [path stroke];

    

    //二次贝塞尔曲线

//    UIColor *color = UMColor(180, 40, 70);

//    [color set];

//    UIBezierPath *path = [UIBezierPath bezierPath];

//    path.lineWidth = 4;

//    path.lineCapStyle = kCGLineCapRound;

//    path.lineJoinStyle = kCGLineCapRound;

//    [path moveToPoint:CGPointMake(20, 300)];

//    [path addQuadCurveToPoint:CGPointMake(190, 200) controlPoint:CGPointMake(150, 100)];

//    [path stroke];

    

    //三次贝塞尔曲线

//    UIColor *color = UMColor(180, 40, 70);

//    [color set];

//    UIBezierPath *path = [UIBezierPath bezierPath];

//    path.lineWidth = 4;

//    path.lineCapStyle = kCGLineCapRound;

//    path.lineJoinStyle = kCGLineCapRound;

//    [path moveToPoint:CGPointMake(20, 300)];

//    [path addCurveToPoint:CGPointMake(250, 250) controlPoint1:CGPointMake(100, 100) controlPoint2:CGPointMake(150, 350)];

//    [path stroke];

    

    //rendering(渲染)Bezier Path对象的内容

//    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 100)];

//    [[UIColor blackColor] setStroke];

//    [[UIColor redColor] setFill];

//    CGContextRef ref = UIGraphicsGetCurrentContext();

//    CGContextTranslateCTM(ref, 50, 50);

//    path.lineWidth = 4;

//    [path stroke];

//    [path fill];

    

    //使用Core Graphics函数去修改path

    //方法一:

//    CGMutablePathRef cgpath = CGPathCreateMutable();

//    CGPathAddEllipseInRect(cgpath, NULL, CGRectMake(0, 0, 300, 250));

//    CGPathAddEllipseInRect(cgpath, NULL, CGRectMake(50, 50, 200, 200));

//    UIBezierPath *path = [UIBezierPath bezierPath];

//    path.CGPath = cgpath;

//    path.usesEvenOddFillRule = YES;

//    CGPathRelease(cgpath);

    

    //方法二:???

//    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 300, 300)];

//    CGPathRef cgpath = path.CGPath;

//    CGMutablePathRef mcgpath = CGPathCreateMutableCopy(cgpath);

//    CGPathAddEllipseInRect(mcgpath, NULL, CGRectMake(50, 50, 200, 200));

//    path.CGPath = mcgpath;

//    CGPathRelease(mcgpath);

//    

//    UIColor *color = [UIColor brownColor];

//    [color set];

//    path.lineWidth = 4;

//    path.lineCapStyle = kCGLineCapRound;

//    path.lineJoinStyle = kCGLineCapRound;

//    [path stroke];

//    [path fill];

    

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    CGContextClearRect(ctx, rect);

    

    

    float angle_start = radians(0.0);

    float angle_end = radians(121.0);

    drawArc(ctx, self.center, angle_start, angle_end, [UIColoryellowColor]);

    

    

    angle_start = angle_end;

    angle_end = radians(228.0);

    drawArc(ctx, self.center, angle_start, angle_end, [UIColorgreenColor]);

    

    

    angle_start = angle_end;

    angle_end = radians(260);

    drawArc(ctx, self.center, angle_start, angle_end, [UIColororangeColor]);

    

    

    angle_start = angle_end;

    angle_end = radians(360);

    drawArc(ctx, self.center, angle_start, angle_end, [UIColorpurpleColor]);


}


/*

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

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/


@end


注意:注释的每一种代表一种方法。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值