iOS 画一个圆展示比例

2019-08-20  本文已影响0人  路边的风景呢

我这里是要用三个参数进行比然后用圆来显示各占百分之多少的样子。我在自定义的View里面写的。

//数组里面装的是字典,,字典里有两个key -> strokeColor和precent

@property (nonatomic,assign) NSArray *circleArray;

//    下面的三个分别代表的是我们这里要展示的三个参数

-(void)drawRectViewWithVip:(float)Vip andBackMoney:(float)BackMoney andClass:(float)Class;

在点M文件里面

__block float a = 0;

    NSArray* circleArray  =@[

                               @{

                                   @"strokeColor":RGBCOLOR(98,178,255),

                                   @"precent":@(Vip/(BackMoney+Class+Vip))//+d+e+f

                                   },

//                              @{

//                                  @"strokeColor":[UIColor whiteColor],

//                                  @"precent":@(d/(BackMoney+Class+Vip+d+e+f))

//                                  },

                               @{

                                   @"strokeColor":RGBCOLOR(255,231,132),

                                   @"precent":@(BackMoney/(BackMoney+Class+Vip))//+d+e+f

                                   },

//                              @{

//                                  @"strokeColor":[UIColor whiteColor],

//                                  @"precent":@(e/(BackMoney+Class+Vip+d+e+f))

//                                  },

                               @{

                                   @"strokeColor":RGBCOLOR(255,175,137),

                                   @"precent":@(Class/(BackMoney+Class+Vip))//+d+e+f

                                   },

//                              @{

//                                  @"strokeColor":[UIColor whiteColor],

//                                  @"precent":@(f/(BackMoney+Class+Vip+d+e+f))

//                                  },

                               ];

    [circleArrayenumerateObjectsUsingBlock:^(NSDictionary*obj,NSUIntegeridx,BOOL*_Nonnullstop) {

        //创建出CAShapeLayer

        self.shapeLayer= [CAShapeLayerlayer];

        self.shapeLayer.frame =CGRectMake(0,0, self.bounds.size.width,self.bounds.size.height);//设置shapeLayer的尺寸和位置

        //    self.shapeLayer.position = self.view.center;

        self.shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色为ClearColor

        //设置线条的宽度和颜色

        self.shapeLayer.lineWidth=10.0f;

        self.shapeLayer.strokeColor= [obj[@"strokeColor"]CGColor];

        //创建出圆形贝塞尔曲线

        UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0, self.bounds.size.width,self.bounds.size.height)];

        //让贝塞尔曲线与CAShapeLayer产生联系

        self.shapeLayer.path= circlePath.CGPath;

        self.shapeLayer.strokeStart= a;

        self.shapeLayer.strokeEnd= [obj[@"precent"]floatValue] + a;

        a =self.shapeLayer.strokeEnd;

        //添加并显示

        [self.layeraddSublayer:self.shapeLayer];

        //添加圆环动画

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

        pathAnimation.duration=1.0;

        pathAnimation.fromValue=@(0);

        pathAnimation.toValue=@(1);

        pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

        [self.shapeLayeraddAnimation:pathAnimationforKey:@"strokeEnd"];

    }];

上一篇 下一篇

猜你喜欢

热点阅读