feat(PieChart): redraw chart if layout changed
This is to support autolayout and constraints. Resize layers for current bounds on layoutSubviews. For that to work we need to have radiuses recomputed on each draw. As a bonus you can now orverride `recompute` method to change inner and outer radiuses. With possibility to change other attributes in a subclass.
Showing
2 changed files
with
23 additions
and
6 deletions
| @@ -44,8 +44,16 @@ | @@ -44,8 +44,16 @@ | ||
| 44 | /** Default YES. */ | 44 | /** Default YES. */ |
| 45 | @property (nonatomic) BOOL shouldHighlightSectorOnTouch; | 45 | @property (nonatomic) BOOL shouldHighlightSectorOnTouch; |
| 46 | 46 | ||
| 47 | +/** Current outer radius. Override recompute() to change this. **/ | ||
| 48 | +@property (nonatomic) CGFloat outerCircleRadius; | ||
| 49 | + | ||
| 50 | +/** Current inner radius. Override recompute() to change this. **/ | ||
| 51 | +@property (nonatomic) CGFloat innerCircleRadius; | ||
| 52 | + | ||
| 47 | @property (nonatomic, weak) id<PNChartDelegate> delegate; | 53 | @property (nonatomic, weak) id<PNChartDelegate> delegate; |
| 48 | 54 | ||
| 49 | - (void)strokeChart; | 55 | - (void)strokeChart; |
| 50 | 56 | ||
| 57 | +- (void)recompute; | ||
| 58 | + | ||
| 51 | @end | 59 | @end |
| @@ -15,9 +15,6 @@ | @@ -15,9 +15,6 @@ | ||
| 15 | @property (nonatomic) NSArray *items; | 15 | @property (nonatomic) NSArray *items; |
| 16 | @property (nonatomic) NSArray *endPercentages; | 16 | @property (nonatomic) NSArray *endPercentages; |
| 17 | 17 | ||
| 18 | -@property (nonatomic) CGFloat outerCircleRadius; | ||
| 19 | -@property (nonatomic) CGFloat innerCircleRadius; | ||
| 20 | - | ||
| 21 | @property (nonatomic) UIView *contentView; | 18 | @property (nonatomic) UIView *contentView; |
| 22 | @property (nonatomic) CAShapeLayer *pieLayer; | 19 | @property (nonatomic) CAShapeLayer *pieLayer; |
| 23 | @property (nonatomic) NSMutableArray *descriptionLabels; | 20 | @property (nonatomic) NSMutableArray *descriptionLabels; |
| @@ -48,9 +45,6 @@ | @@ -48,9 +45,6 @@ | ||
| 48 | self = [self initWithFrame:frame]; | 45 | self = [self initWithFrame:frame]; |
| 49 | if(self){ | 46 | if(self){ |
| 50 | _items = [NSArray arrayWithArray:items]; | 47 | _items = [NSArray arrayWithArray:items]; |
| 51 | - _outerCircleRadius = CGRectGetWidth(self.bounds) / 2; | ||
| 52 | - _innerCircleRadius = CGRectGetWidth(self.bounds) / 6; | ||
| 53 | - | ||
| 54 | _descriptionTextColor = [UIColor whiteColor]; | 48 | _descriptionTextColor = [UIColor whiteColor]; |
| 55 | _descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:18.0]; | 49 | _descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:18.0]; |
| 56 | _descriptionTextShadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.4]; | 50 | _descriptionTextShadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.4]; |
| @@ -86,12 +80,20 @@ | @@ -86,12 +80,20 @@ | ||
| 86 | 80 | ||
| 87 | _pieLayer = [CAShapeLayer layer]; | 81 | _pieLayer = [CAShapeLayer layer]; |
| 88 | [_contentView.layer addSublayer:_pieLayer]; | 82 | [_contentView.layer addSublayer:_pieLayer]; |
| 83 | + | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +/** Override this to change how inner attributes are computed. **/ | ||
| 87 | +- (void)recompute { | ||
| 88 | + self.outerCircleRadius = CGRectGetWidth(self.bounds) / 2; | ||
| 89 | + self.innerCircleRadius = CGRectGetWidth(self.bounds) / 6; | ||
| 89 | } | 90 | } |
| 90 | 91 | ||
| 91 | #pragma mark - | 92 | #pragma mark - |
| 92 | 93 | ||
| 93 | - (void)strokeChart{ | 94 | - (void)strokeChart{ |
| 94 | [self loadDefault]; | 95 | [self loadDefault]; |
| 96 | + [self recompute]; | ||
| 95 | 97 | ||
| 96 | PNPieChartDataItem *currentItem; | 98 | PNPieChartDataItem *currentItem; |
| 97 | for (int i = 0; i < _items.count; i++) { | 99 | for (int i = 0; i < _items.count; i++) { |
| @@ -443,4 +445,11 @@ | @@ -443,4 +445,11 @@ | ||
| 443 | [squareImageView setFrame:CGRectMake(originX, originY, size, size)]; | 445 | [squareImageView setFrame:CGRectMake(originX, originY, size, size)]; |
| 444 | return squareImageView; | 446 | return squareImageView; |
| 445 | } | 447 | } |
| 448 | + | ||
| 449 | +/* Redraw the chart on autolayout */ | ||
| 450 | +-(void)layoutSubviews { | ||
| 451 | + [super layoutSubviews]; | ||
| 452 | + [self strokeChart]; | ||
| 453 | +} | ||
| 454 | + | ||
| 446 | @end | 455 | @end |
-
Please register or login to post a comment