ios-貝茲路徑

來源:互聯網
上載者:User

標籤:alpha   pen   shape   min   cts   att   minus   貝塞爾   3.0   

git:[email protected]:lu459700780/UIBezierPath.git

示範:

 

#import "ViewController.h"@interface ViewController ()@property (nonatomic,assign)BOOL Plus_Minus;@property (nonatomic,strong)CAShapeLayer * shapeLayer;@property (nonatomic,strong)UIBezierPath * trackPath;@property (nonatomic,strong)CAShapeLayer * trackLayer;@property (nonatomic,strong)UIBezierPath * progressPath;@property (nonatomic,strong)CAShapeLayer * progressLayer;@property (nonatomic,strong)NSTimer * timer;@property (nonatomic,strong)CAShapeLayer * shapeLayerT;@property (nonatomic,strong)CAShapeLayer * shapeLayerTM;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    _Plus_Minus = YES;        //建立CAShapeLayer    self.shapeLayer = [CAShapeLayer layer];    self.shapeLayer.frame = CGRectMake(100, 100, 50, 50);//    self.shapeLayer.position = self.view.center;    self.shapeLayer.fillColor = [UIColor cyanColor].CGColor;//填充顏色為ClearColor        //設定線條的寬度和顏色    self.shapeLayer.lineWidth = 1.0f;    self.shapeLayer.strokeColor = [UIColor redColor].CGColor;    //建立出圓形貝茲路徑    UIBezierPath * circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 50, 50)];        //讓貝茲路徑與CAShapeLayer 產生聯絡    self.shapeLayer.path = circlePath.CGPath;        //一、添加並顯示 畫一個圓形//    [self.view.layer addSublayer:self.shapeLayer];        /*     Stroke:用筆畫的意思     在這裡就是起始筆和結束筆的位置     Stroke為1的話就是一整圈,0.5就是半圈,0.25就是1/4圈。以此類推     */    self.shapeLayer.strokeStart = 0;    self.shapeLayer.strokeEnd = 0.75;        //二、畫兩個圓,其中一個圓表示進度    [self createBezierPath:CGRectMake(100, 200, 100, 100)];    //三、建立轉動的圓    [self circleBezierPath];    //四、通過點畫線組成一個五邊線    [self fiveAnimation];    //五、畫一條虛線    [self createDottedLine];    //六、畫一個弧線    [self createCurvedLine];}-(void)createCurvedLine{            UIBezierPath* aPath = [UIBezierPath bezierPath];    aPath.lineWidth = 5.0;    aPath.lineCapStyle = kCGLineCapRound; //線條拐角    aPath.lineJoinStyle = kCGLineCapRound; //終點處理    [aPath moveToPoint:CGPointMake(20, 100)];    [aPath addQuadCurveToPoint:CGPointMake(120, 100) controlPoint:CGPointMake(70, 0)];        CAShapeLayer * CurvedLineLayer=[CAShapeLayer layer];    CurvedLineLayer.path=aPath.CGPath;    [self.view.layer addSublayer:CurvedLineLayer];}-(void)createDottedLine{            CAShapeLayer *shapeLayer = [CAShapeLayer layer];    [shapeLayer setBounds:self.view.bounds];    [shapeLayer setPosition:self.view.center];    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];        // 設定虛線顏色為blackColor    [shapeLayer setStrokeColor:[[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0f] CGColor]];        // 3.0f設定虛線的寬度    [shapeLayer setLineWidth:1.0f];    [shapeLayer setLineJoin:kCALineJoinRound];        // 3=線的寬度 1=每條線的間距    [shapeLayer setLineDashPattern:     [NSArray arrayWithObjects:[NSNumber numberWithInt:3],      [NSNumber numberWithInt:1],nil]];        // Setup the path    CGMutablePathRef path = CGPathCreateMutable();    CGPathMoveToPoint(path, NULL, 0, 89);    CGPathAddLineToPoint(path, NULL, 320,89);        [shapeLayer setPath:path];    CGPathRelease(path);        // 可以把self改成任何你想要的UIView, 示範就是放到UITableViewCell中的    [[self.view layer] addSublayer:shapeLayer];}//五角形-(void)fiveAnimation{        UIBezierPath *aPath = [UIBezierPath bezierPath];    //開始點 從上左下右的點    [aPath moveToPoint:CGPointMake(100,100)];    //劃線點    [aPath addLineToPoint:CGPointMake(60, 140)];    [aPath addLineToPoint:CGPointMake(60, 240)];    [aPath addLineToPoint:CGPointMake(160, 240)];    [aPath addLineToPoint:CGPointMake(160, 140)];    [aPath closePath];    //設定定點是個5*5的小圓形(自己加的)    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100-5/2.0, 50, 20, 20)];    [aPath appendPath:path];        CAShapeLayer *shapelayer = [CAShapeLayer layer];    //設定邊框顏色    shapelayer.strokeColor = [[UIColor redColor]CGColor];    //設定填充顏色 如果只要邊 可以把裡面設定成[UIColor ClearColor]    shapelayer.fillColor = [[UIColor blueColor]CGColor];    //就是這句話在關聯彼此(UIBezierPath和CAShapeLayer):    shapelayer.path = aPath.CGPath;    [self.view.layer addSublayer:shapelayer];}-(void)circleBezierPath{        _timer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(circleAnimationTypeOne) userInfo:nil repeats:YES];        self.shapeLayerT = [CAShapeLayer layer];    self.shapeLayerT.frame = CGRectMake(0, 0, 150, 150);    self.shapeLayerT.position = self.view.center;    self.shapeLayerT.fillColor = [UIColor clearColor].CGColor;        //設定線條的寬度和顏色    self.shapeLayerT.lineWidth = 2.0f;    self.shapeLayerT.strokeColor = [UIColor redColor].CGColor;    //設定stroke起始點    self.shapeLayerT.strokeStart = 0;    self.shapeLayerT.strokeEnd = 0;        //建立出圓形貝茲路徑    UIBezierPath * circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 150, 150)];        //讓貝茲路徑與CAShapeLayer產生聯絡    self.shapeLayerT.path = circlePath.CGPath;    //添加並顯示    [self.view.layer addSublayer:self.shapeLayerT];    self.shapeLayerTM = [CAShapeLayer layer];    self.shapeLayerTM.frame = CGRectMake(0, 0, 170, 170);    self.shapeLayerTM.position = self.view.center;    self.shapeLayerTM.fillColor = [UIColor clearColor].CGColor;        //設定線條的寬度和顏色    self.shapeLayerTM.lineWidth = 2.0f;    self.shapeLayerTM.strokeColor = [UIColor blueColor].CGColor;        //設定stroke起始點    self.shapeLayerTM.strokeStart = 0;    self.shapeLayerTM.strokeEnd = 0;        //建立出圓形貝茲路徑    UIBezierPath * circlePathTM = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 170, 170)];        //讓貝茲路徑與CAShapeLayer產生聯絡    self.shapeLayerTM.path = circlePathTM.CGPath;    //添加並顯示    [self.view.layer addSublayer:self.shapeLayerTM];}-(void)circleAnimationTypeOne{            NSLog(@"strokeStart===%f",self.shapeLayerT.strokeStart);    NSLog(@"strokeEnd====%f",self.shapeLayerT.strokeEnd);    if (self.shapeLayerT.strokeEnd > 1 && self.shapeLayerT.strokeStart < 1) {                self.shapeLayerT.strokeStart += 0.1;        self.shapeLayerTM.strokeStart += 0.1;    }else if(self.shapeLayerT.strokeStart == 0){                self.shapeLayerT.strokeEnd += 0.1;        self.shapeLayerTM.strokeEnd += 0.1;    }        if (self.shapeLayerT.strokeEnd == 0) {                self.shapeLayerT.strokeStart = 0;        self.shapeLayerTM.strokeStart = 0;    }    if (self.shapeLayerT.strokeStart == self.shapeLayerT.strokeEnd) {                self.shapeLayerT.strokeEnd = 0;        self.shapeLayerTM.strokeEnd = 0;    }    }//畫兩個圓形-(void)createBezierPath:(CGRect)mybound{            //外圓    _trackPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:(mybound.size.width-0.7)/2 startAngle:0 endAngle:M_PI*2 clockwise:YES];    _trackLayer = [CAShapeLayer new];    [self.view.layer addSublayer:_trackLayer];    _trackLayer.fillColor = nil;    _trackLayer.strokeColor = [UIColor grayColor].CGColor;    _trackLayer.path = _trackPath.CGPath;    _trackLayer.lineWidth = 5;    _trackLayer.frame = mybound;        //內圓    _progressPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:(mybound.size.width-0.7)/2 startAngle:-M_PI_2 endAngle:(M_PI*2)*0.7 clockwise:YES];    _progressLayer = [CAShapeLayer new];    [self.view.layer addSublayer:_progressLayer];    _progressLayer.fillColor = nil;    _progressLayer.strokeColor = [UIColor redColor].CGColor;    _progressLayer.lineCap = kCALineCapRound;    _progressLayer.path = _progressPath.CGPath;    _progressLayer.lineWidth = 5;    _progressLayer.frame = mybound;        }@end

 

ios-貝茲路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.