【问题标题】:Animating circular progress View in Swift在 Swift 中动画循环进度视图
【发布时间】:2015-10-26 08:55:55
【问题描述】:

我想用下面的代码为我的循环progressView 制作动画。我希望动画从 0 开始进度到我想要的值。 但我永远无法获得动画作品。

func Popular(){
let circle = Pop;//Pop is a UIView

circle.bounds = CGRect(x: 0,y: 0, width: 100, height: 100);
circle.frame = CGRectMake(0,0, 100, 100);

circle.layoutIfNeeded()

var progressCircle = CAShapeLayer();

let centerPoint = CGPoint (x: circle.bounds.width / 2, y: circle.bounds.width / 2);
let circleRadius : CGFloat = circle.bounds.width / 2 * 0.83;

let circlePath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: CGFloat(-0.5 * M_PI), endAngle: CGFloat(1.5 * M_PI), clockwise: true    );

progressCircle = CAShapeLayer ();
progressCircle.path = circlePath.CGPath;
progressCircle.strokeColor = UIColor.greenColor().CGColor;
progressCircle.fillColor = UIColor.clearColor().CGColor;

    UIView.animateWithDuration(3.0,
        delay: 1.0,
        options: [],
        animations: {
            progressCircle.lineWidth = 5.0;
            progressCircle.strokeStart = 0;
            progressCircle.strokeEnd = 0.20;

            circle.layer.addSublayer(progressCircle);

            circle

            progressCircle.strokeEnd = 0.2;

        },
        completion: { finished in
            print("Picutre done")


    })


}

【问题讨论】:

  • 您是否收到任何错误消息?当你运行它时会发生什么?
  • 完全没有错误信息...

标签: ios swift animation progress


【解决方案1】:

使用CABasicAnimation

    let circle = Pop

    var progressCircle = CAShapeLayer()

    let circlePath = UIBezierPath(ovalInRect: circle.bounds)

    progressCircle = CAShapeLayer ()
    progressCircle.path = circlePath.CGPath
    progressCircle.strokeColor = UIColor.greenColor().CGColor
    progressCircle.fillColor = UIColor.clearColor().CGColor
    progressCircle.lineWidth = 5.0

    circle.layer.addSublayer(progressCircle)


    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = 0
    animation.toValue = 0.2
    animation.duration = 3
    animation.fillMode = kCAFillModeForwards
    animation.removedOnCompletion = false

    progressCircle.addAnimation(animation, forKey: "ani")

【讨论】:

  • 我还有一个问题是圆圈大小总是大于视图。如何调整?
  • 让 circlePath = UIBezierPath(ovalInRect: CGRectInset(circle.bounds, 5 / 2.0, 5 / 2.0))
【解决方案2】:

两件事——

首先-不要重新初始化progressCircle。你打电话给progressCircle = CAShapeLayer (); 两次。不过,这不是错误。

第二 - 在开始制作动画之前添加您的子图层。将circle.layer.addSublayer(progressCircle); 移出动画块。

【讨论】:

  • 谢谢。我按照你的建议改了代码,唯一的动画就是突然出现了progressCircle。我已经把代码放在 ViewDidAppear 中了。
  • 是的。 CABasicAnimation 是动画子层的正确方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2021-12-01
  • 2023-03-21
  • 2016-08-25
  • 2017-07-27
  • 1970-01-01
相关资源
最近更新 更多