如何自定义矩形进度条?

时间:2013-04-01 10:14:51

标签: ios objective-c progress-bar uiprogressview uiprogressbar

我想用UIKIT定制一个矩形进度条,就像吸引的图像一样。有没有任何源代码? Cocos2d与CCProgressTimer具有相同的功能,但我找不到任何UIKIT源代码。 Rectangle progress bar

2 个答案:

答案 0 :(得分:7)

创建ShapedLayer

CAShapeLayer *layer = [CAShapeLayer layer];
[layer setStrokeColor:[UIColor greenColor].CGColor];
[layer setLineWidth:10.0f];

[layer setFillColor:[UIColor clearColor].CGColor];

创建一个带有你想要动画的radious的矩形

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 300, 200) cornerRadius:10.0f];
layer.path = path.CGPath;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat:1.0f];

定义动画持续时间

animation.duration = 4.0f;
[layer addAnimation:animation forKey:@"myStroke"];

将动画添加到要显示的图层

[self.view.layer addSublayer:layer];

答案 1 :(得分:0)

以下是SWIFT 2.0中UIButton边框动画的代码

func animation(){
        CATransaction.begin()

        let layer : CAShapeLayer = CAShapeLayer()
        layer.strokeColor = UIColor.whiteColor().CGColor
        layer.lineWidth = 3.0
        layer.fillColor = UIColor.clearColor().CGColor

        let path : UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 84, height: 30), byRoundingCorners: .AllCorners, cornerRadii: CGSize(width: 5.0, height: 0.0))
        layer.path = path.CGPath

        let animation : CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0.0
        animation.toValue = 1.0

        animation.duration = 4.0

        CATransaction.setCompletionBlock{ [weak self] in
            if self!.isAdvClick == false {
                self!.navController?.popViewControllerAnimated(false)
            }
        }

        layer.addAnimation(animation, forKey: "myStroke")
        CATransaction.commit()
        self.btnSkip.layer.addSublayer(layer)
    }

您可以查看此内容 Link