Skip to content

Instantly share code, notes, and snippets.

@benjaminsnorris
Last active August 29, 2015 14:15
Show Gist options
  • Save benjaminsnorris/1d78961ec6554f0d0d72 to your computer and use it in GitHub Desktop.
Save benjaminsnorris/1d78961ec6554f0d0d72 to your computer and use it in GitHub Desktop.
Filling Segmented Custom View
//
// FillingSegmentedCircleView.h
// Created by Ben Norris on 2/20/15.
// Copyright (c) 2015 BSN Design. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface FillingSegmentedCircleView : UIView
@property (nonatomic, assign) NSInteger segments;
@property (nonatomic, assign) double lineWidth;
@property (nonatomic, assign) double progress;
@end
//
// FillingSegmentedCircleView.m
// Created by Ben Norris on 2/20/15.
// Copyright (c) 2015 BSN Design. All rights reserved.
//
#import "FillingSegmentedCircleView.h"
@interface FillingSegmentedCircleView()
@property (nonatomic, strong) CAShapeLayer *backgroundRingLayer;
@property (nonatomic, strong) CAShapeLayer *ringLayer;
@end
@implementation FillingSegmentedCircleView
- (void)layoutSubviews {
[super layoutSubviews];
if (!self.backgroundRingLayer) {
self.backgroundRingLayer = [CAShapeLayer layer];
[self.layer addSublayer:self.backgroundRingLayer];
CGRect rect = CGRectInset(self.bounds, self.lineWidth / 2.0, self.lineWidth / 2.0);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
self.backgroundRingLayer.path = path.CGPath;
self.backgroundRingLayer.fillColor = nil;
self.backgroundRingLayer.lineWidth = self.lineWidth;
self.backgroundRingLayer.strokeColor = [UIColor blackColor].CGColor;
self.backgroundRingLayer.lineDashPattern = [self getLineDashPatternForRect:self.bounds withSegments:self.segments];
}
self.backgroundRingLayer.frame = self.layer.bounds;
if (!self.ringLayer) {
self.ringLayer = [CAShapeLayer layer];
CGRect innerRect = CGRectInset(self.bounds, self.lineWidth / 2.0, self.lineWidth / 2.0);
UIBezierPath *innerPath = [UIBezierPath bezierPathWithOvalInRect:innerRect];
self.ringLayer.path = innerPath.CGPath;
self.ringLayer.fillColor = nil;
self.ringLayer.lineWidth = self.lineWidth;
self.ringLayer.strokeColor = [UIColor blueColor].CGColor;
self.ringLayer.lineDashPattern = [self getLineDashPatternForRect:self.bounds withSegments:self.segments];
self.ringLayer.anchorPoint = CGPointMake(0.5, 0.5);
self.ringLayer.transform = CATransform3DRotate(self.ringLayer.transform, -M_PI/2, 0, 0, 1);
[self.layer addSublayer:self.ringLayer];
}
self.ringLayer.frame = self.layer.bounds;
[self updateLayerProperties];
}
- (NSArray *)getLineDashPatternForRect:(CGRect)rect withSegments:(NSUInteger)segments {
CGFloat circumference = M_PI * rect.size.width;
CGFloat segmentSpacing = 2.0;
CGFloat totalSegmentSpacingLength = segmentSpacing * (segments);
CGFloat segmentLength = (circumference - totalSegmentSpacingLength) / segments;
return @[@(segmentLength), @(segmentSpacing)];
}
- (void)updateLayerProperties {
if (self.ringLayer) {
self.ringLayer.strokeEnd = self.progress;
}
}
- (void)setProgress:(double)progress {
_progress = progress;
[self updateLayerProperties];
}
@end
https://developer.apple.com/videos/wwdc/2014/#411-video
What's New in Interface Builder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment