暂停和恢复圆形动画

Le'Kirdok

我使用此代码绘制圆圈。圆圈将在30秒内绘制出来。有没有暂停和恢复绘制圆的方法?我可以暂停时间,但是找不到暂停和恢复绘图圈的方法。

 int radius = 30;
 circle = [CAShapeLayer layer];
 // Make a circular shape
 circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 100, 2.0*radius, 2.0*radius)
                                          cornerRadius:radius].CGPath;
 // Center the shape in self.view
 circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
                               CGRectGetMidY(self.view.frame)-radius);

 // Configure the apperence of the circle
 circle.fillColor = [UIColor lightGrayColor].CGColor;
 circle.strokeColor = [UIColor orangeColor].CGColor;
 circle.lineWidth = 10;


 // Add to parent layer
 [container.layer addSublayer:circle];

 // Configure animation
 drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
 drawAnimation.duration            = 30.0; // "animate over 10 seconds or so.."
 drawAnimation.repeatCount         = 1.0;  // Animate only once..
 drawAnimation.removedOnCompletion = NO;   // Remain stroked after the animation...


 // Animate from no part of the stroke being drawn to the entire stroke being drawn
 drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
 drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];

 // Experiment with timing to get the appearence to look the way you want
 drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

 // Add the animation to the circle
 [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
马特·朗

您是否曾从Apple看到过有关暂停动画的技术说明:https : //developer.apple.com/library/ios/qa/qa1673/_index.html

因此,对于您的项目,类似这样的方法可以解决问题:

- (void)viewDidLoad
{
    [super viewDidLoad];

    int radius = 30;
    _circle = [CAShapeLayer layer];
    // Make a circular shape
    _circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 100, 2.0*radius, 2.0*radius)
                                              cornerRadius:radius].CGPath;
    // Center the shape in self.view
    _circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
                                   CGRectGetMidY(self.view.frame)-radius);

    // Configure the apperence of the circle
    _circle.fillColor = [UIColor lightGrayColor].CGColor;
    _circle.strokeColor = [UIColor orangeColor].CGColor;
    _circle.lineWidth = 10;

    _circle.strokeEnd = 0.0f;

    // Add to parent layer
    [self.view.layer addSublayer:_circle];

}

- (IBAction)didTapPlayPauseButton:(id)sender
{
    if (!_drawAnimation) {
        [self addAnimation];
    } else if(_circle.speed == 0){
        [self resumeLayer:_circle];
    } else {
        [self pauseLayer:_circle];
    }

}

- (void)addAnimation
{
    // Configure animation
    _drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    _drawAnimation.duration            = 30.0; // "animate over 10 seconds or so.."
    _drawAnimation.repeatCount         = 1.0;  // Animate only once..
    _drawAnimation.removedOnCompletion = NO;   // Remain stroked after the animation...


    // Animate from no part of the stroke being drawn to the entire stroke being drawn
    _drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    _drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];

    // Experiment with timing to get the appearence to look the way you want
    _drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    // Add the animation to the circle
    [_circle addAnimation:_drawAnimation forKey:@"drawCircleAnimation"];
}

- (void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

- (void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

我将它扔到Github上的一个项目中,看它是否可以工作:https : //github.com/perlmunger/PauseAnimation.git

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何暂停和恢复动画-KDCircularProgress圆形栏

来自分类Dev

暂停和恢复书脊动画

来自分类Dev

摇摆动画暂停和恢复

来自分类Dev

摇摆动画暂停和恢复

来自分类Dev

使用JS暂停,恢复和重新启动Canvas动画

来自分类Dev

暂停和恢复功能

来自分类Dev

暂停和恢复BackgroundWorker

来自分类Dev

暂停和恢复下载

来自分类Dev

jQuery动画暂停恢复速度变慢

来自分类Dev

响应式CSS3链动画,暂停和恢复问题

来自分类Dev

暂停和恢复每n秒发生的SpriteBatch动画的最佳方法

来自分类Dev

SpriteKit暂停和恢复SKView

来自分类Dev

Libgdx中的暂停和恢复

来自分类Dev

Swift:暂停和恢复NSTimer

来自分类Dev

如何暂停和恢复流程

来自分类Dev

播放和暂停Flutter动画

来自分类Dev

暂停和恢复SwingWorker.doInBackground()

来自分类Dev

如何暂停和恢复jQuery间隔

来自分类Dev

状态恢复,如何拦截和暂停SKScene

来自分类Dev

停止,中断,暂停和恢复Java线程

来自分类Dev

暂停和恢复线程活动

来自分类Dev

如何暂停和恢复UIView.animateWithDuration

来自分类Dev

JMeter-按需暂停(和恢复)执行

来自分类Dev

在SparkStreaming中暂停和恢复KafkaConsumer

来自分类Dev

状态恢复,如何拦截和暂停SKScene

来自分类Dev

停止,中断,暂停和恢复Java线程

来自分类Dev

如何暂停和恢复旋转木马滑块

来自分类Dev

暂停和恢复CasperJS,PhantomJS脚本

来自分类Dev

是否可以暂停和恢复AVPlayer的缓冲?