簡體   English   中英

如何自定義矩形進度條?

[英]How to custom rectangle progress bar?

我想用 UIKIT 自定義一個矩形進度條,比如吸引的圖像。 有沒有這方面的源代碼? Cocos2d 與 CCProgressTimer 具有相同的功能,但我找不到任何 UIKIT 源代碼。矩形進度條

創建一個 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];

斯威夫特 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)
    }

您可以在此鏈接上查看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM