自訂進度條,android自訂進度條

來源:互聯網
上載者:User

自訂進度條,android自訂進度條

用 CAShapeLayer 可以根據傳入的的貝茲路徑UIBezierPath *path , 建立出需要的進度條形狀。然後只要 即時的設定CAShapeLayer的strokeEnd屬性,就可以更新進度條的進度。

部分代碼:

/** 進度條 和 背景 */- (CAShapeLayer *)createLayer:(UIColor *)strokeColor path:(UIBezierPath *)path {    CAShapeLayer *layer = [CAShapeLayer layer];    layer.lineWidth = 5;    layer.fillColor = [UIColor clearColor].CGColor;    layer.strokeColor = strokeColor.CGColor;    layer.path= path.CGPath;    [self.layer addSublayer:layer];    return layer;}/** 設定進度 描邊停止 */- (void)setProgress:(CGFloat)progress {    _progress = progress;        self.progressLabel.text = [NSString stringWithFormat:@"%.0f%%",progress *100];    self.progressLayer.strokeEnd = _progress;}/** 建立貝茲路徑 */- (UIBezierPath *)createPathWith:(CGRect)frame {        UIBezierPath *path = [UIBezierPath bezierPath];        switch (self.style) {        case CusProgressViewStyleNone:{            path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(frame.size.width / 2.0, frame.size.height / 2.0) radius:CGRectGetWidth(frame)/ 2.0 startAngle:-M_PI_2 endAngle:M_PI_2 * 3 clockwise:YES];        }            break;        case CusProgressViewStyleRectSegment: {            CGFloat minAngle = 2 * M_PI / self.gridCount;            for (int i = 0; i < self.gridCount; i++) {                                CGFloat startAngle = i * minAngle;                CGFloat endAngle = (i+ 2/3.0) * minAngle;                                UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(frame.size.width / 2.0, frame.size.height / 2.0) radius:CGRectGetWidth(frame)/ 2.0 startAngle:-M_PI_2 + startAngle endAngle:-M_PI_2 + endAngle clockwise:YES];                [path appendPath:path1];            }        }            break;        case CusProgressViewStyleRoundSegment: {            self.backgroundLayer.lineCap = kCALineCapRound;            self.backgroundLayer.lineJoin = kCALineJoinRound;            self.progressLayer.lineCap = kCALineCapRound;            self.progressLayer.lineJoin = kCALineJoinRound;                        static CGFloat minAngle = 1;            for (int i = 0; i < ceil(360 / (minAngle + self.gridInterval)); i++) {                                CGFloat startAngle = i * (minAngle+self.gridInterval) * M_PI / 180.0;                CGFloat endAngle = startAngle + minAngle * M_PI / 180.0;                if (endAngle >= 2 * M_PI) {                    endAngle = 2 * M_PI;                }                                UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(frame.size.width / 2.0, frame.size.height / 2.0) radius:CGRectGetWidth(frame)/ 2.0 startAngle:-M_PI_2 + startAngle endAngle:-M_PI_2 + endAngle clockwise:YES];                [path appendPath:path2];            }        }            break;        default:            break;    }        return path;}

 

練習代碼:http://pan.baidu.com/s/1mhxGEuc

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.