Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中设置循环进度视图的动画_Ios_Swift_Animation_Progress - Fatal编程技术网

Ios 在Swift中设置循环进度视图的动画

Ios 在Swift中设置循环进度视图的动画,ios,swift,animation,progress,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 p

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


    })


}
有两件事-

首先-不要重新初始化
progressCircle
。您正在调用
progressCircle=CAShapeLayer()两次。但这不是bug


第二步-在开始设置动画之前添加子层。移动
circle.layer.addSublayer(progressCircle)动画块外部。

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

您是否收到任何错误消息?运行此操作时会发生什么?完全没有错误消息…谢谢。我根据你的建议更改了代码,唯一的动画是progressCircle突然出现。我已经将代码放入ViewDidAppear.Yes
CABasicAnimation
是制作子层动画的正确方法。我还有一个问题,就是圆的大小总是比视图大。如何调整它?让circlePath=UIBezierPath(ovalInRect:CGRectInset(circle.bounds,5/2.0,5/2.0))