gpt4 book ai didi

ios - 顺利停止 CABasicAnimation

转载 作者:行者123 更新时间:2023-11-29 12:46:54 27 4
gpt4 key购买 nike

我正在旋转一个 UIBarButtonItem。动画效果很好。但是,当我从服务器接收到数据时,我想顺利地停止动画。

我尝试使用 presentationLayer 捕获当前值,但我得到的只是 0。

- (void)animateLeftBarButtonItem
{
//Animate Button
CABasicAnimation *leftBarButtonItemRotator = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
leftBarButtonItemRotator.delegate = self;
leftBarButtonItemRotator.removedOnCompletion = NO;
leftBarButtonItemRotator.fillMode = kCAFillModeForwards;
leftBarButtonItemRotator.duration = 5.0;
leftBarButtonItemRotator.fromValue = @0.0f;
leftBarButtonItemRotator.toValue = @(50*(-2.0f * M_PI));
leftBarButtonItemRotator.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.navigationItem.leftBarButtonItem.customView.layer addAnimation:leftBarButtonItemRotator forKey:@"leftBarButtonRotation"];

}

有什么建议吗?

问候

最佳答案

好吧,我非常需要这个,我花了一个小时尝试不同的技巧。

下一个想法似乎可行。它基于这样一个事实,即似乎每个动画都添加到一个层(即使它作用于相同的属性),CA 混合它。所以我们开始另一个具有相同属性的动画,因为我们希望它们淡出。当它完成时,我们删除之前的。

我接下来提供的示例中有几个关键部分。我把它们加粗了。

希望对您有所帮助。

-(void)slowBeat{    //check if animations alredy running?    [CATransaction begin];    CAKeyframeAnimation* zoom = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];    zoom.duration = 1.0f;    zoom.speed = .75f;    zoom.repeatCount = INFINITY;    zoom.fillMode = kCAFillModeForwards;    zoom.removedOnCompletion = YES;    zoom.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];    zoom.calculationMode = kCAAnimationCubicPaced;    zoom.keyTimes = [NSArray arrayWithObjects:                     [NSNumber numberWithFloat:0.0f],                     [NSNumber numberWithFloat:0.80f],                     [NSNumber numberWithFloat:0.95f],                     [NSNumber numberWithFloat:1.0f], nil];    zoom.values = [NSArray arrayWithObjects:                   [NSNumber numberWithFloat:1.0f],                   [NSNumber numberWithFloat:1.0f],                   [NSNumber numberWithFloat:1.3f],                   [NSNumber numberWithFloat:1.0f], nil];    [self.contentWrapper.layer addAnimation:zoom forKey:@"slowBeatPosition"];    CAKeyframeAnimation* fadeInAndOut = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];    fadeInAndOut.duration = 1.0f;    fadeInAndOut.speed = .75f;    fadeInAndOut.repeatCount = INFINITY;    fadeInAndOut.fillMode = kCAFillModeForwards;    fadeInAndOut.removedOnCompletion = YES;    fadeInAndOut.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];    fadeInAndOut.calculationMode = kCAAnimationCubicPaced;    fadeInAndOut.keyTimes = [NSArray arrayWithObjects:                             [NSNumber numberWithFloat:0.0f],                             [NSNumber numberWithFloat:0.80f],                             [NSNumber numberWithFloat:0.95f],                             [NSNumber numberWithFloat:1.0f], nil];    fadeInAndOut.values = [NSArray arrayWithObjects:                           (id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor,                           (id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor,                           (id)[UIColor colorWithWhite:1.0f alpha:0.7f].CGColor,                           (id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor, nil];    [self.redView.layer addAnimation:fadeInAndOut forKey:@"slowBeatColor"];    [CATransaction commit];    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        [CATransaction begin];        [CATransaction setCompletionBlock:^{            [self.contentWrapper.layer removeAnimationForKey:@"slowBeatPosition"];            [self.redView.layer removeAnimationForKey:@"slowBeatColor"];            [self.contentWrapper.layer removeAnimationForKey:@"slowBeatPosition2"];            [self.redView.layer removeAnimationForKey:@"slowBeatColor2"];        }];        CABasicAnimation* zoom2 = [CABasicAnimation animationWithKeyPath:@"transform"];        zoom2.duration = 0.4f;        zoom2.speed = 1.0f;        zoom2.repeatCount = 1;        zoom2.fillMode = kCAFillModeForwards;        zoom2.removedOnCompletion = NO;        zoom2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];        zoom2.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];        [self.contentWrapper.layer addAnimation:zoom2 forKey:@"slowBeatPosition2"];        CABasicAnimation* fadeInAndOut2 = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];        fadeInAndOut2.duration = 0.4f;        fadeInAndOut2.speed = 1.0f;        fadeInAndOut2.repeatCount = 1;        fadeInAndOut2.fillMode = kCAFillModeForwards;        fadeInAndOut2.removedOnCompletion = NO;        fadeInAndOut2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];        fadeInAndOut2.toValue = (id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor;        [self.redView.layer addAnimation:fadeInAndOut2 forKey:@"slowBeatColor2"];        [CATransaction commit];    });}

关于ios - 顺利停止 CABasicAnimation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23471995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com