Ios 如何自定义矩形进度条?

Ios 如何自定义矩形进度条?,ios,objective-c,progress-bar,uiprogressview,uiprogressbar,Ios,Objective C,Progress Bar,Uiprogressview,Uiprogressbar,我想定制一个矩形进度条,带有类似UIKIT的图片。有没有这方面的源代码? Cocos2d与CCProgressTimer具有相同的功能,但我找不到任何UIKIT源代码。 创建一个成型层 CAShapeLayer *layer = [CAShapeLayer layer]; [layer setStrokeColor:[UIColor greenColor].CGColor]; [layer setLineWidth:10.0f]; [layer setFillColor:[UIColor cl

我想定制一个矩形进度条,带有类似UIKIT的图片。有没有这方面的源代码? Cocos2d与CCProgressTimer具有相同的功能,但我找不到任何UIKIT源代码。 创建一个成型层

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];
SWIFT 5版本

以下是swift 2.0中用于UIButton边框动画的代码


您可以在这个上查看put

谢谢您的代码。成功了。但如何将路径的起点设置在中间上方?我跟你不太熟CAAnimation@SukhmeetHora彼此彼此。你找到解决方法了吗?我也需要同样的方法,以及如何改变起点和终点。。!可以使用CATTransferm3dMakeRotation在每个角落开始旋转图层。嘿,Toandk,你不应该回答这个问题吗?你说它奏效了;在答案上做标记将拯救其他看问题的人,并给迪彭应得的荣誉
    func animation() {
    CATransaction.begin()
    
    let layer : CAShapeLayer = CAShapeLayer()
    layer.strokeColor = UIColor.white.cgColor
    layer.lineWidth = 3.0
    layer.fillColor = UIColor.clear.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!.navigationController?.popViewController(animated: false)
        }
    }
    
    layer.add(animation, forKey: "myStroke")
    CATransaction.commit()
    self.btnSkip.layer.addSublayer(layer)
}
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)
    }