|
| 1 | +// |
| 2 | +// MPBarsGraphView.m |
| 3 | +// MPPlot |
| 4 | +// |
| 5 | +// Created by Alex Manzella on 22/05/14. |
| 6 | +// Copyright (c) 2014 mpow. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "MPBarsGraphView.h" |
| 10 | + |
| 11 | +@implementation MPBarsGraphView |
| 12 | + |
| 13 | + |
| 14 | +- (id)initWithFrame:(CGRect)frame |
| 15 | +{ |
| 16 | + self = [super initWithFrame:frame]; |
| 17 | + if (self) { |
| 18 | + // Initialization code |
| 19 | + self.backgroundColor=[UIColor clearColor]; |
| 20 | + |
| 21 | + currentTag=-1; |
| 22 | + |
| 23 | + self.topCornerRadius=-1; |
| 24 | + } |
| 25 | + return self; |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +- (void)drawRect:(CGRect)rect |
| 31 | +{ |
| 32 | + [super drawRect:rect]; |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + if (self.values.count && !self.waitToUpdate) { |
| 37 | + |
| 38 | + [label removeFromSuperview]; label=nil; |
| 39 | + |
| 40 | + for (UIView *subview in self.subviews) { |
| 41 | + [subview removeFromSuperview]; |
| 42 | + } |
| 43 | + |
| 44 | + [self addBarsAnimated:shouldAnimate]; |
| 45 | + |
| 46 | + [self.graphColor setStroke]; |
| 47 | + |
| 48 | + UIBezierPath *line=[UIBezierPath bezierPath]; |
| 49 | + |
| 50 | + [line moveToPoint:CGPointMake(PADDING, self.height)]; |
| 51 | + [line addLineToPoint:CGPointMake(self.width-PADDING, self.height)]; |
| 52 | + [line setLineWidth:1]; |
| 53 | + [line stroke]; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +- (void)addBarsAnimated:(BOOL)animated{ |
| 58 | + |
| 59 | + |
| 60 | + for (UIButton* button in buttons) { |
| 61 | + [button removeFromSuperview]; |
| 62 | + } |
| 63 | + |
| 64 | + buttons=[[NSMutableArray alloc] init]; |
| 65 | + |
| 66 | + if (animated) { |
| 67 | + self.layer.masksToBounds=YES; |
| 68 | + } |
| 69 | + |
| 70 | + CGFloat barWidth=self.width/(points.count*2+1); |
| 71 | + CGFloat radius=barWidth*(self.topCornerRadius >=0 ? self.topCornerRadius : 0.3); |
| 72 | + for (NSInteger i=0;i<points.count;i++) { |
| 73 | + |
| 74 | + CGFloat height=[[points objectAtIndex:i] floatValue]*(self.height-PADDING*2)+PADDING; |
| 75 | + |
| 76 | + UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom]; |
| 77 | + [button setBackgroundColor:self.graphColor]; |
| 78 | + button.frame=CGRectMake(barWidth+(barWidth*i+barWidth*i), animated ? self.height : self.height-height, barWidth, animated ? height+20 : height); |
| 79 | + |
| 80 | + |
| 81 | + CAShapeLayer *maskLayer = [CAShapeLayer layer]; |
| 82 | + maskLayer.frame = button.bounds; |
| 83 | + |
| 84 | + |
| 85 | + maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(radius, radius)].CGPath; |
| 86 | + |
| 87 | + button.layer.mask=maskLayer; |
| 88 | + |
| 89 | + [button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside]; |
| 90 | + button.tag=i; |
| 91 | + [self addSubview:button]; |
| 92 | + |
| 93 | + if (animated) { |
| 94 | + [UIView animateWithDuration:self.animationDuration delay:i*0.1 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ |
| 95 | + button.y=self.height-height-20; |
| 96 | + }completion:^(BOOL finished) { |
| 97 | + [UIView animateWithDuration:.15 animations:^{ |
| 98 | + button.frame=CGRectMake(barWidth+(barWidth*i+barWidth*i), self.height-height, barWidth, height); |
| 99 | + }]; |
| 100 | + }]; |
| 101 | + } |
| 102 | + |
| 103 | + [buttons addObject:button]; |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + shouldAnimate=NO; |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +- (CGFloat)animationDuration{ |
| 117 | + return _animationDuration>0.0 ? _animationDuration : .25; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +- (void)tap:(UIButton *)button{ |
| 123 | + |
| 124 | + |
| 125 | + if (label.superview) { |
| 126 | + __block UILabel *labelBK=label; |
| 127 | + [UIView animateWithDuration:.15 animations:^{ |
| 128 | + labelBK.transform=CGAffineTransformMakeScale(0, 0); |
| 129 | + }completion:^(BOOL finished) { |
| 130 | + [labelBK removeFromSuperview]; labelBK=nil; |
| 131 | + }]; |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + if (button.tag==currentTag) { |
| 136 | + currentTag=-1; |
| 137 | + return; |
| 138 | + }else currentTag=button.tag; |
| 139 | + |
| 140 | + label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 20)]; |
| 141 | + label.textAlignment=NSTextAlignmentCenter; |
| 142 | + label.textColor=self.detailTextColor ? self.detailTextColor : ( self.graphColor ); |
| 143 | + label.backgroundColor=self.detailBackgroundColor; |
| 144 | + label.layer.borderColor=label.textColor.CGColor; |
| 145 | + label.layer.borderWidth=.5; |
| 146 | + if ([[self.values objectAtIndex:currentTag] isKindOfClass:[NSString class]]) { |
| 147 | + label.text=[self.values objectAtIndex:currentTag]; |
| 148 | + }else label.text=[NSString stringWithFormat:@"%@",[self.detailLabelFormatter stringFromNumber:[self.values objectAtIndex:currentTag]]]; |
| 149 | + label.center=CGPointMake(button.center.x,button.center.y); |
| 150 | + label.layer.cornerRadius=3; |
| 151 | + label.clipsToBounds=YES; |
| 152 | + label.transform=CGAffineTransformMakeScale(0, 0); |
| 153 | + [self addSubview:label]; |
| 154 | + [UIView animateWithDuration:.2 animations:^{ |
| 155 | + label.transform=CGAffineTransformMakeScale(1, 1); |
| 156 | + }]; |
| 157 | + |
| 158 | + |
| 159 | +} |
| 160 | + |
| 161 | + |
| 162 | +- (void)animate{ |
| 163 | + |
| 164 | + self.waitToUpdate=NO; |
| 165 | + |
| 166 | + shouldAnimate=YES; |
| 167 | + |
| 168 | + [self setNeedsDisplay]; |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | +@end |
0 commit comments