CAShapeLayer的属性 path, 配合上 UIBezierPath(贝塞尔曲线)使用

来源:互联网 发布:淘宝店铺全屏轮播海报 编辑:程序博客网 时间:2024/06/05 14:49
////  ViewController.m//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    /*     1.CAShapeLayer的属性 path, 配合上 UIBezierPath(贝塞尔曲线)使用。     2.fillColor 和 strokeColor ,前者代表设置这个 Layer 的填充色,后者代表设置它的边框色     3.贝塞尔曲线的画法是由起点、终点、控制点三个参数来画的     4.控制点决定了它的曲率,曲线的顶点不等于控制点的位置,具体可以看一下贝塞尔曲线的定义     */}// 一:带边框的view- (IBAction)TestOne:(id)sender {    UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];    CAShapeLayer * layer = [CAShapeLayer layer];    layer.fillColor = [[UIColor grayColor] CGColor];    layer.strokeColor =[[UIColor greenColor] CGColor];    layer.path = path.CGPath;    [self.view.layer addSublayer:layer];}// 二:圆框- (IBAction)TestTwo:(id)sender {    UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, self.view.center.y) radius:50 startAngle:0 endAngle:2*M_PI clockwise:true];    CAShapeLayer * layer = [CAShapeLayer layer];    layer.fillColor = [[UIColor clearColor] CGColor];    layer.strokeColor =[[UIColor greenColor] CGColor];    layer.path = path.CGPath;    [self.view.layer addSublayer:layer];}// 三:曲线- (IBAction)TestThree:(id)sender {    CGPoint startPoint = CGPointMake(50, 500);    CGPoint endPoint = CGPointMake(250, 500);    CGPoint controlPoint = CGPointMake(150, 400);    UIBezierPath * path = [UIBezierPath bezierPath];    [path moveToPoint:startPoint];    [path addQuadCurveToPoint:endPoint controlPoint:controlPoint];    CAShapeLayer * layer = [CAShapeLayer layer];    layer.path = path.CGPath;    layer.fillColor = [[UIColor clearColor] CGColor];    layer.strokeColor =[[UIColor greenColor] CGColor];    [self.view.layer addSublayer:layer];}@end

这里写图片描述

效果图

这里写图片描述

阅读全文
0 0