131 lines
4.0 KiB
Objective-C
131 lines
4.0 KiB
Objective-C
//
|
|
// JBBaseChartViewController.m
|
|
// JBChartViewDemo
|
|
//
|
|
// Created by Terry Worona on 3/13/14.
|
|
// Copyright (c) 2014 Jawbone. All rights reserved.
|
|
//
|
|
|
|
#import "JBBaseChartViewController.h"
|
|
|
|
// Views
|
|
#import "JBChartTooltipTipView.h"
|
|
|
|
// Numerics
|
|
CGFloat const kJBBaseChartViewControllerAnimationDuration = 0.25f;
|
|
|
|
@interface JBBaseChartViewController ()
|
|
|
|
@property (nonatomic, strong) JBChartTooltipView *tooltipView;
|
|
@property (nonatomic, strong) JBChartTooltipTipView *tooltipTipView;
|
|
|
|
@end
|
|
|
|
@implementation JBBaseChartViewController
|
|
|
|
#pragma mark - Setters
|
|
|
|
- (void)setTooltipVisible:(BOOL)tooltipVisible animated:(BOOL)animated atTouchPoint:(CGPoint)touchPoint
|
|
{
|
|
_tooltipVisible = tooltipVisible;
|
|
|
|
JBChartView *chartView = [self chartView];
|
|
|
|
if (!chartView)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!self.tooltipView)
|
|
{
|
|
self.tooltipView = [[JBChartTooltipView alloc] init];
|
|
self.tooltipView.alpha = 0.0;
|
|
[self.view addSubview:self.tooltipView];
|
|
}
|
|
|
|
if (!self.tooltipTipView)
|
|
{
|
|
self.tooltipTipView = [[JBChartTooltipTipView alloc] init];
|
|
self.tooltipTipView.alpha = 0.0;
|
|
[self.view addSubview:self.tooltipTipView];
|
|
}
|
|
|
|
dispatch_block_t adjustTooltipPosition = ^{
|
|
CGPoint originalTouchPoint = [self.view convertPoint:touchPoint fromView:chartView];
|
|
CGPoint convertedTouchPoint = originalTouchPoint; // modified
|
|
JBChartView *chartView = [self chartView];
|
|
if (chartView)
|
|
{
|
|
CGFloat minChartX = (chartView.frame.origin.x + ceil(self.tooltipView.frame.size.width * 0.5));
|
|
if (convertedTouchPoint.x < minChartX)
|
|
{
|
|
convertedTouchPoint.x = minChartX;
|
|
}
|
|
CGFloat maxChartX = (chartView.frame.origin.x + chartView.frame.size.width - ceil(self.tooltipView.frame.size.width * 0.5));
|
|
if (convertedTouchPoint.x > maxChartX)
|
|
{
|
|
convertedTouchPoint.x = maxChartX;
|
|
}
|
|
self.tooltipView.frame = CGRectMake(convertedTouchPoint.x - ceil(self.tooltipView.frame.size.width * 0.5), CGRectGetMaxY(chartView.headerView.frame), self.tooltipView.frame.size.width, self.tooltipView.frame.size.height);
|
|
|
|
CGFloat minTipX = (chartView.frame.origin.x + self.tooltipTipView.frame.size.width);
|
|
if (originalTouchPoint.x < minTipX)
|
|
{
|
|
originalTouchPoint.x = minTipX;
|
|
}
|
|
CGFloat maxTipX = (chartView.frame.origin.x + chartView.frame.size.width - self.tooltipTipView.frame.size.width);
|
|
if (originalTouchPoint.x > maxTipX)
|
|
{
|
|
originalTouchPoint.x = maxTipX;
|
|
}
|
|
self.tooltipTipView.frame = CGRectMake(originalTouchPoint.x - ceil(self.tooltipTipView.frame.size.width * 0.5), CGRectGetMaxY(self.tooltipView.frame), self.tooltipTipView.frame.size.width, self.tooltipTipView.frame.size.height);
|
|
}
|
|
};
|
|
|
|
dispatch_block_t adjustTooltipVisibility = ^{
|
|
self.tooltipView.alpha = _tooltipVisible ? 1.0 : 0.0;
|
|
self.tooltipTipView.alpha = _tooltipVisible ? 1.0 : 0.0;
|
|
};
|
|
|
|
if (tooltipVisible)
|
|
{
|
|
adjustTooltipPosition();
|
|
}
|
|
|
|
if (animated)
|
|
{
|
|
[UIView animateWithDuration:kJBBaseChartViewControllerAnimationDuration animations:^{
|
|
adjustTooltipVisibility();
|
|
} completion:^(BOOL finished) {
|
|
if (!tooltipVisible)
|
|
{
|
|
adjustTooltipPosition();
|
|
}
|
|
}];
|
|
}
|
|
else
|
|
{
|
|
adjustTooltipVisibility();
|
|
}
|
|
}
|
|
|
|
- (void)setTooltipVisible:(BOOL)tooltipVisible animated:(BOOL)animated
|
|
{
|
|
[self setTooltipVisible:tooltipVisible animated:animated atTouchPoint:CGPointZero];
|
|
}
|
|
|
|
- (void)setTooltipVisible:(BOOL)tooltipVisible
|
|
{
|
|
[self setTooltipVisible:tooltipVisible animated:NO];
|
|
}
|
|
|
|
#pragma mark - Getters
|
|
|
|
- (JBChartView *)chartView
|
|
{
|
|
// Subclasses should return chart instance for tooltip functionality
|
|
return nil;
|
|
}
|
|
|
|
@end
|