Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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 仅水平设置CAGradientLayer的动画_Ios_Objective C_Calayer_Caanimation_Cagradientlayer - Fatal编程技术网

Ios 仅水平设置CAGradientLayer的动画

Ios 仅水平设置CAGradientLayer的动画,ios,objective-c,calayer,caanimation,cagradientlayer,Ios,Objective C,Calayer,Caanimation,Cagradientlayer,我正在尝试设置CAGradientLayer(渐变和帧)的动画,当我将帧的动画设置为0时,它会收缩到视图的左上角,我希望它只是水平地设置动画。现在它在水平和垂直方向上都设置为0 以下是动画当前的外观: 我想要的是当它下降到零的时候,只是水平收缩,并且一直保持相同的高度 以下是我当前使用的代码: - (void)drawRect:(CGRect)rect { // Drawing code if (!gradientLayer) { gradientLayer = [CAGradient

我正在尝试设置CAGradientLayer(渐变和帧)的动画,当我将帧的动画设置为0时,它会收缩到视图的左上角,我希望它只是水平地设置动画。现在它在水平和垂直方向上都设置为0

以下是动画当前的外观:

我想要的是当它下降到零的时候,只是水平收缩,并且一直保持相同的高度

以下是我当前使用的代码:

- (void)drawRect:(CGRect)rect
{
// Drawing code
if (!gradientLayer) {
    gradientLayer = [CAGradientLayer layer];
    [gradientLayer setFrame:[self createRectForFrame:rect]];
    [gradientLayer setColors:@[(id)fromColor.CGColor, (id)toColor.CGColor]];
    [gradientLayer setStartPoint:CGPointMake(fillPct/100, 0.5)];
    [gradientLayer setEndPoint:CGPointMake(1.0, 0.5)];
    [self.layer addSublayer:gradientLayer];
}
else {
    [gradientLayer removeAllAnimations];

    [CATransaction begin];

    [CATransaction setAnimationDuration:ANIMATION_DURATION];
    CGRect newFrame = [self createRectForFrame:rect];
    CABasicAnimation *frameAnimation = [CABasicAnimation animationWithKeyPath:@"frame"];
    [frameAnimation setDuration:ANIMATION_DURATION];
    [frameAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [frameAnimation setFromValue:[NSValue valueWithCGRect:gradientLayer.bounds]];
    [frameAnimation setToValue:[NSValue valueWithCGRect:newFrame]];
    [frameAnimation setFillMode:kCAFillModeForwards];
    [frameAnimation setRemovedOnCompletion:NO];
    [gradientLayer setFrame:newFrame];
    [gradientLayer addAnimation:frameAnimation forKey:@"frame"];

    CGPoint startPoint = CGPointMake(MIN(fillPct/100, 0.99), 0.5);
    CABasicAnimation *startPointAnimation = [CABasicAnimation animationWithKeyPath:@"startPoint"];
    [startPointAnimation setDuration:ANIMATION_DURATION];
    [startPointAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [startPointAnimation setFromValue:[NSValue valueWithCGPoint:gradientLayer.startPoint]];
    [startPointAnimation setToValue:[NSValue valueWithCGPoint:startPoint]];
    [startPointAnimation setFillMode:kCAFillModeForwards];
    [startPointAnimation setRemovedOnCompletion:NO];
    [gradientLayer setStartPoint:startPoint];
    [gradientLayer addAnimation:startPointAnimation forKey:@"startPoint"];

    [CATransaction setCompletionBlock:^{
        [self updateInnerLabelTextColor];
    }];

    [CATransaction commit];
}
[self bringInnerLabelToFront];
}

- (CGRect)createRectForFrame:(CGRect)frame {
if (fillPct == 0)
    return CGRectZero;
else if (fillPct >= 100)
    return frame;
else
    return CGRectMake(frame.origin.x, frame.origin.y, frame.size.width * (fillPct / 100), frame.size.height);
}

fillPct是条形图应填充的值,介于0和100之间。

问题在于这一行:

CGPoint startPoint = CGPointMake(MIN(fillPct/100, 0.99), 0.5);
如果将其更改为:

CGPoint startPoint = CGPointMake(0.0, 0.5);
它可以根据你的需要工作——不会在拐角处退缩

或者,如果希望保留当前代码,更改此值也可以:

CGPoint startPoint = CGPointMake(MIN(fillPct/100, 0.0), 0.5);
刚刚测试过,;它在水平方向上设置动画。希望这有帮助

更新1:

我真的没有遇到你在评论中提到的问题;但是,我决定再看一遍。由于您没有为
rect
提供任何任意值以及您反复调用它的方式,因此我提出了自己的方法,并再次对其进行了测试

我使用此值设置了
gradientLayer

[gradientLayer setFrame:(CGRect){{150.0f, 240.0f}, 150.0f, 20.0f}];
在第一个
CABasicAnimation
中,出于测试目的,我更改了以下内容:

[frameAnimation setToValue:[NSValue valueWithCGRect:(CGRect){{150.0f, 240.0f}, 0.0f, 20.0f}]];
如您所见,它与层的rect完全相同,只是宽度为0.0f。想象一下,这将导致宽度为0

上述方法有效吗?对

以上工作方式是否符合您的要求?没有

原因是,这将导致帧均匀收缩-这不是您想要的

因此,在创建
渐变层时添加以下内容:

gradientLayer.anchorPoint = (CGPoint){0.0f, 0.5f};
再一次,我测试了它,在我写作的这一刻,它在我面前运行。顺便说一下,这很有趣


希望这也能有所帮助。

我尝试更改该行,但当fillPct值=0(从createRectForFrame函数返回CGRectZero)时,它仍然收缩到角部,而不是水平收缩。drawRect不是该代码的正确位置,因为它不使用核心图形,而是核心动画