Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带UIBezierPath的动画_Ios_Objective C_Animation_Uibezierpath_Custom View - Fatal编程技术网

Ios 带UIBezierPath的动画

Ios 带UIBezierPath的动画,ios,objective-c,animation,uibezierpath,custom-view,Ios,Objective C,Animation,Uibezierpath,Custom View,我搜索了很多这个主题,所有的答案都说我必须使用CAShapeLayer或Layers和。问题是我真的不知道如何使用它们,我知道解决问题的方法;我只是想知道,如果没有现金支付者,是否有办法做到这一点 我正在尝试制作一个星形的评级视图。有一颗星星,里面有一个从下到上的矩形,表示额定星星的百分比 这是我的.h文件: IB_DESIGNABLE @interface FSStarRatingView : UIView @property (assign, nonatomic) IBInspectabl

我搜索了很多这个主题,所有的答案都说我必须使用CAShapeLayer或Layers和。问题是我真的不知道如何使用它们,我知道解决问题的方法;我只是想知道,如果没有现金支付者,是否有办法做到这一点

我正在尝试制作一个星形的评级视图。有一颗星星,里面有一个从下到上的矩形,表示额定星星的百分比

这是我的.h文件:

IB_DESIGNABLE
@interface FSStarRatingView : UIView

@property (assign, nonatomic) IBInspectable BOOL transparentInside;
@property (assign, nonatomic) IBInspectable CGFloat innerRadius; // between 0-1

@property (assign, nonatomic) IBInspectable CGFloat percentage; // between 0-1
@property (strong, nonatomic) IBInspectable UIColor *fillColor;
@property (strong, nonatomic) IBInspectable UIColor *unfilledColor; // works if transparentInside is YES
@property (strong, nonatomic) IBInspectable UIColor *strokeColor;

- (void)setPercentage:(CGFloat)percentage withAnimationDuration:(CGFloat)duration;
这是我的.m文件

- (UIBezierPath *)starShapedBezierPath {
    CGPoint points[10];
    //  points[0] = CGPointMake(self.center.x, 0);
    CGFloat outterRadius = self.frame.size.width / 2;
    CGFloat startDegree = 3.0 * M_PI_2;
    CGPoint center = CGPointMake(outterRadius, outterRadius * 2.0/1.85);
    for (int i = 0; i < 10; i+=2) {
        points[i] = CGPointMake(center.x + outterRadius * cos(startDegree),
                                center.y + outterRadius * sin(startDegree));
        startDegree += 0.4 * M_PI;
    }

    CGFloat innerRadius = self.innerRadius * outterRadius;
    startDegree = M_PI_2;
    for (int i = 5; i < 15; i+=2) {
        points[i%10] = CGPointMake(center.x + innerRadius * cos(startDegree),
                                   center.y + innerRadius * sin (startDegree));
        startDegree += 0.4 * M_PI;
    }

    UIBezierPath *star = [UIBezierPath bezierPath];
    [star moveToPoint:points[0]];
    for (int i = 1; i < 10; i++)
        [star addLineToPoint:points[i]];
    [star closePath];
    return star;
}

- (void)setPercentage:(CGFloat)percentage {
    _percentage = percentage;
    [self setNeedsDisplay];
}

- (void)setPercentage:(CGFloat)percentage withAnimationDuration:(CGFloat)duration{
    [UIView
     animateWithDuration:duration
     animations:^{
         self.percentage = percentage;
     }];
}

- (void)drawRect:(CGRect)rect {
    UIBezierPath *star = [self starShapedBezierPath];
    star.lineWidth = 1.0;
    [self.strokeColor setStroke];
    [star stroke];
    [star addClip];
    CGFloat topBottomOffset = 0.15 * self.frame.size.width / 4.0;
    CGFloat availableHeight = self.frame.size.height - 2.0 * topBottomOffset;
    CGFloat usedHeight = availableHeight * self.percentage;
    if (! self.transparentInside) {
        UIBezierPath *unusedPercentageRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, topBottomOffset, self.frame.size.width, availableHeight - usedHeight)];
        [self.unfilledColor setFill];
        [unusedPercentageRect fill];
    }

    UIBezierPath *usedPercentageRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, topBottomOffset + availableHeight - usedHeight, self.frame.size.width, usedHeight)];
    [self.fillColor setFill];
    [usedPercentageRect fill];
    [star addClip];
}
-(UIBezierPath*)星形BezierPath{
CGPoint点[10];
//点[0]=CGPointMake(self.center.x,0);
CGFloat outterRadius=self.frame.size.width/2;
CGFloat startDegree=3.0*M_PI_2;
CGPoint center=CGPointMake(地球外,地球外*2.0/1.85);
对于(int i=0;i<10;i+=2){
点[i]=CGPointMake(中心x+外场*cos(起点度),
中心y+outterRadius*sin(标准度));
起点坡度+=0.4*M_π;
}
CGFloat innerRadius=self.innerRadius*outterRadius;
startDegree=M_PI_2;
对于(int i=5;i<15;i+=2){
点[i%10]=CGPointMake(中心x+内半径*cos(起始角度),
中心y+内半径*sin(起始角度));
起点坡度+=0.4*M_π;
}
UIBezierPath*星形=[UIBezierPath bezierPath];
[星形移动点:点[0]];
对于(int i=1;i<10;i++)
[star addLineToPoint:points[i]];
[星光大道];
返回星;
}
-(无效)设置百分比:(CGFloat)百分比{
_百分比=百分比;
[自我设置需要显示];
}
-(void)setPercentage:(CGFloat)带有动画持续时间的百分比:(CGFloat)持续时间{
[UIView
animateWithDuration:持续时间
动画:^{
self.percentage=百分比;
}];
}
-(void)drawRect:(CGRect)rect{
UIBezierPath*星形=[自星形BezierPath];
星形。线宽=1.0;
[self.strokeColor setStroke];
[星击];
[star addClip];
CGFloat topBottomOffset=0.15*self.frame.size.width/4.0;
CGFloat可用高度=self.frame.size.height-2.0*topBottomOffset;
CGFloat usedHeight=可用重量*自身百分比;
如果(!self.transparentInside){
UIBezierPath*unusedPercentageRect=[UIBezierPath bezierPathWithRect:CGRectMake(0,上下偏移量,self.frame.size.width,可用高度-使用高度)];
[self.unfledcolor setFill];
[unusedPercentageRect fill];
}
UIBezierPath*usedPercentageRect=[UIBezierPath bezierPathWithRect:CGRectMake(0,上下偏移量+可用高度-使用高度,self.frame.size.width,使用高度)];
[self.fillColor setFill];
[使用百分比填充];
[star addClip];
}
我知道有可能用2个子视图和动画制作它们的帧,只是想知道是否有一种方法可以直接用BezierPath来做。。。
谢谢你是说这种事吗?这正是我的意思@mattWell,那就这样吧。我的答案这就是你想要的答案。很抱歉,我仍然不知道怎么做@matt,我在那里读了你的答案,你说要么使用CAShapeLayer,要么使用自定义动画属性。这就是我“认为”我在这里所做的,但显然我不是。我想使用百分比作为自定义的可设置动画的属性,每当百分比改变时,绘制的图片就会改变。我以为它会成功,但它不会:(自定义可动画属性:你是说这类东西吗?这正是我的意思@mattWell,那你就来吧。我的答案就是你想要的答案。对不起,但我仍然不知道如何做@matt,我在那里读了你的答案,你说要么使用CAShapeLayer,要么使用自定义可动画属性。这就是我的“想法”我在这里做,但显然不是。我想使用百分比作为自定义动画属性,每次百分比改变时,绘制的图片都会改变。我想它会做到,但不会:(自定义动画属性: