0

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

4

2 回答 2

9

创建一个 ShapedLayer

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

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

在您想要动画的地方创建一个带有radius的矩形

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];
于 2013-04-01T10:27:19.357 回答
1

斯威夫特 5 版

    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)
}

这是 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)
    }

您可以在此 链接上查看

于 2018-04-23T06:16:45.643 回答