ios - 在 Swift 中动画循环进度 View

标签 ios swift animation progress

我想用下面的代码为我的循环 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")


    })


}

最佳答案

使用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")

关于ios - 在 Swift 中动画循环进度 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33341794/

相关文章:

swift - UIButton 配置不更新按钮标题

ios - 在 becomeFirstResponder 上禁用 UISearchBar 搜索图标和占位符文本动画

c# - 不使用 OnPaint 则无效

javascript - 如何通过单击鼠标更改文本区域的高度

ios - 触摸并拖动 UIButton,但在松开手指时不触发它

android - react native - "undefined is not an object react native evaluating this.props.navigation"

ios - 每 10 个单元格重复 TableView

ios - 我可以在 Swift 中使用静态下标吗?

android - 我的服务器可以主动向我的 iPhone/Android 应用程序发送数据吗?

ios - 自定义 plist 文件中的变量