ios 简单折线图

缘分已到,这是一个简单的折线图,带阴影有点击事件,有刷新 ,回调写好了,不行就自己加点,改点吧,人之患好为人师,所以我就简单说两句啊~<>~。

我做的是这样事儿的

ios 简单折线图_第1张图片


首先是贝尔曲线

///设置 起始点 到 移动点  这个只是路径

UIBezierPath*path = [UIBezierPathbezierPath];

[path moveToPoint:CGPointMake(GapBetweenX* t +tableX,0+tableY)];

[path addLineToPoint:CGPointMake(GapBetweenX* t +tableX,(_arrCoordinatesY.count-1)*GapBetweenY+tableY)];

[path closePath];

这个才是 路径展现出来的方法 

CAShapeLayer*shapeLayerX = [CAShapeLayer layer];

shapeLayerX.path= path.CGPath;

shapeLayerX.strokeColor= colorX.CGColor;

shapeLayerX.lineWidth=0.5;

[_scrollView.layer addSublayer:shapeLayerX];

其实这两段 就是 iOS 划线的核心方法了,折线图什么的修修改改就好了,再见。。。。。。。。。。。。













您能看到这儿就表明,道友你我有缘啊,再见。

好了,折线图 就是一条线,利用 moveToPoint:  和  addLineToPoint :  被分成好多段

大概就是这样

for(int t =0; t <_arrBrokenLine.count; t ++) {

CGFloat lineY = [[_arrBrokenLine objectAtIndex:t]floatValue];

CGFloat lineX =GapBetweenX*t +tableX;

CGFloat coordinatesY = (tableHight)/(maxY - minY) * (maxY - lineY) +tableY;

CGPoint point =CGPointMake(lineX , coordinatesY);

if(t !=_arrBrokenLine.count-1) {

CGFloatlineYNext = [[_arrBrokenLine objectAtIndex:t +1]floatValue] ;

CGFloat lineXNext =GapBetweenX* (t +1) +tableX;

CGFloat coordinatesYNext =tableHight/(maxY - minY) * (maxY - lineYNext) +tableY;

CGPoint pointNext =CGPointMake(lineXNext , coordinatesYNext );

[path addLineToPoint:pointNext];

[path moveToPoint:pointNext];

}

[pathLayer addLineToPoint:point];

if(t ==_arrBrokenLine.count-1) {

[pathLayer addLineToPoint:CGPointMake(point.x,_scrollView.contentSize.height-5)];

}

(好气啊,复制过来空格都没了)

想要加个动画如下

CABasicAnimation*pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

pathAnimation.duration=2;

pathAnimation.timingFunction= [CAMediaTiming FunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

pathAnimation.fromValue= [NSNumber numberWithFloat:0.0f];

pathAnimation.toValue= [NSNumber numberWithFloat:1.0f];

pathAnimation.autoreverses=NO;

pathAnimation.delegate=self;

[shapeLayerX addAnimation:pathAnimationforKey:@"strokeEndAnimation"];

shapeLayerX.strokeEnd=1.0;

如果你还想加个背景

///背景图layer

CAGradientLayer*gradLayer = [CAGradientLayerlayer];

gradLayer.frame=CGRectMake(0,0,_scrollView.contentSize.width,_scrollView.contentSize.height);

gradLayer.colors=@[(__bridgeid)RGBCOLOR_ALPHA(47,131,228,0.3).CGColor,(__bridgeid)RGBCOLOR_ALPHA(47,131,228,0.3).CGColor,(__bridgeid)RGBCOLOR_ALPHA(47,131,228,0.3).CGColor];

gradLayer.startPoint=CGPointMake(0,1);

gradLayer.endPoint=CGPointMake(0,1);

gradLayer.locations=@[@0.2,@0.5,@0.7];

[_scrollView.layeraddSublayer:gradLayer];

写了这么多 ,我估计也没有几个人会仔细看

注意这里放链接了,有兴趣的可以一起做些更好的动画特效什么的 github.com/zdqwsx/BrokenLine

差点忘记 祝我自己身体健康,明天就有美女表白。

你可能感兴趣的:(ios 简单折线图)