你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios 繪制曲線走勢圖

ios 繪制曲線走勢圖

編輯:IOS開發綜合

- (void)drawLine{
    
    //view是曲線的背景view
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 300, 300)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];

    //第一、UIBezierPath繪制線段
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    //四個點
    CGPoint point = CGPointMake(10, 10);
    CGPoint point1 = CGPointMake(200, 100);
    CGPoint point2 = CGPointMake(240, 200);
    CGPoint point3 = CGPointMake(290, 200);
    
    NSArray *arr = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:point],[NSValue valueWithCGPoint:point1],[NSValue valueWithCGPoint:point2],[NSValue valueWithCGPoint:point3], nil];
    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, arr.count)];
   //第二、就是這句話繪制
    [arr enumerateObjectsAtIndexes:set options:0 usingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL *stop){
        
        CGPoint point = [pointValue CGPointValue];
        [path addLineToPoint:point];
        
        //(一)rect折線畫法
        CGRect rect;
        rect.origin.x = point.x - 1.5;
        rect.origin.y = point.y - 1.5;
        rect.size.width = 4;
        rect.size.height = 4;
        
        //(二)rect射線畫法
//        CGRect rect = CGRectMake(10, 10, 1, 1);
        
        UIBezierPath *arc= [UIBezierPath bezierPathWithOvalInRect:rect];
        [path appendPath:arc];
    }];
    //第三、UIBezierPath和CAShapeLayer關聯
    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.frame = CGRectMake(0, 150, 320, 400);
    lineLayer.fillColor = [UIColor redColor].CGColor;
    lineLayer.path = path.CGPath;
    lineLayer.strokeColor = [UIColor redColor].CGColor;
    [view.layer addSublayer:lineLayer];
    
    //以下代碼為附加的
    //(一)像一個幕布一樣拉開,顯得有動畫
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 320, 400)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    
    [UIView animateWithDuration:5 animations:^{
        view1.frame = CGRectMake(320, 100, 320, 400);
    }];
 
}



  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved