• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 暂停和恢复圆形动画

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:37:52 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我用这段代码来画圆。圆圈在 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"];



Best Answer-推荐答案


您是否看过 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)didTapPlayPauseButtonid)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)pauseLayerCALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

- (void)resumeLayerCALayer*)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

关于ios - 暂停和恢复圆形动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20570961/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap