#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *money;

@end
#import "ViewController.h"
#import "DrawView.h"

@interface ViewController () <DrawViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DrawView *drawView = [[DrawView alloc] init];
    drawView.backgroundColor = [UIColor whiteColor];
    drawView.array = @[@2222.4,@2225.4,@1853.4,@2320.4,@2100.4,@2056.4,@2135.4,@2265.4,@2485.4,@1985.4];
    drawView.frame = CGRectMake(15, 42, [drawView.array count] * 50 - 50, 450);
    drawView.delegate = self;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 100, 300, 500)];
    scrollView.contentSize = CGSizeMake([drawView.array count] * 50 - 20, 500);
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.bounces = NO;
    [scrollView addSubview:drawView];
    [self.view addSubview:scrollView];
    
    scrollView.backgroundColor = [UIColor redColor];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)btnChange:(DrawView *)drawView{
    
    self.money.text = drawView.money;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

----------------------------------------------

#import <UIKit/UIKit.h>

@class DrawView;
@protocol DrawViewDelegate <NSObject>

@optional
- (void)btnChange:(DrawView *)drawView;

@end

@interface DrawView : UIView
{
    CAGradientLayer *gradient;
}

@property (nonatomic,weak) id<DrawViewDelegate> delegate;
@property (nonatomic,copy) NSArray    *array;
@property (nonatomic,copy) NSString   *money;
@property (nonatomic,strong) UIImageView *arrow;

@end
#import "DrawView.h"

#define XLine    25

@interface DrawView ()
{
    NSArray   *_days;
}
@end

@implementation DrawView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    gradient = [CAGradientLayer layer];
    gradient.frame = self.bounds;
    gradient.colors = @[(__bridge id)[UIColor colorWithRed:75.0 / 255.0 green:160.0 / 255.0 blue:234.0 / 255.0 alpha:1].CGColor,(__bridge id)[UIColor whiteColor].CGColor];;
    [self.layer addSublayer:gradient];
    
    if (_days == nil) {
        _days = @[@"08-01",@"08-02",@"08-03",@"08-04",@"08-05",@"08-06",@"08-07",@"08-08",@"08-09",@"08-10"];
    }
    
    double distanceNextYline = 50;     //x间距
    double bizieLineAligm = 30;        //影响曲线弯曲幅度
    double nextX = 0;
    double downToXline = self.frame.size.height - XLine; //曲线范围高度
    
    CGFloat min,max;
    min=max=[[_array firstObject] floatValue];
    for (NSInteger i=0; i<_array.count; i++) {
        
        CGFloat val=[[_array objectAtIndex:i] floatValue];
        if (val>max) {
            max=val;
        }
        if (val<min) {
            min=val;
        }
    }
    double dValue = min;
    double coefficient;
    double corectPointer1;
    //系数
    if (max == min) {
        coefficient = 0;
        corectPointer1 = downToXline;
    }else{
        coefficient = downToXline / (max - dValue);
        corectPointer1 = downToXline - ([_array[0] doubleValue] - dValue )*coefficient;
    }

    UIColor *currentColor = [UIColor blackColor];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextSetLineWidth(context, 1.0);
    CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
    
    // line X
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:153.0f / 255.0f green:153.0f / 255.0f blue:153.0f / 255.0f alpha:1].CGColor);
    CGContextMoveToPoint(context, 0, downToXline);
    
    UILabel *day = [[UILabel alloc] init];
    day.backgroundColor = [UIColor clearColor];
    day.textAlignment = NSTextAlignmentCenter;
    [day setFont:[UIFont systemFontOfSize:9]];
    [day setAdjustsFontSizeToFitWidth:YES];
    day.text = [NSString stringWithFormat:@"%@",[_days objectAtIndex:0]];
    day.textColor = [UIColor colorWithRed:255.0f / 255.0f green:123.0f / 255.0f blue:0 alpha:1];
    
    CGSize daySize = [day.text sizeWithAttributes:@{NSFontAttributeName:day.font}];
    day.frame = CGRectMake(0, 0, daySize.width, daySize.height);
    day.center = CGPointMake(0, downToXline + 12);
    [self addSubview:day];
    
    for (int i = 1; i<_array.count; i++) {
        
        CGContextAddLineToPoint(context, distanceNextYline * i, downToXline);
        CGContextAddLineToPoint(context, distanceNextYline * i, downToXline + 4);
        CGContextAddLineToPoint(context, distanceNextYline * i, downToXline);
        
        UILabel *day = [[UILabel alloc] init];
        day.backgroundColor = [UIColor clearColor];
        day.textAlignment = NSTextAlignmentCenter;
        [day setFont:[UIFont systemFontOfSize:9]];
        [day setAdjustsFontSizeToFitWidth:YES];
        day.text = [NSString stringWithFormat:@"%@",[_days objectAtIndex:i]];
        day.textColor = [UIColor colorWithRed:255.0f / 255.0f green:123.0f / 255.0f blue:0 alpha:1];
        
        CGSize daySize = [day.text sizeWithAttributes:@{NSFontAttributeName:day.font}];
        day.frame = CGRectMake(0, 0, daySize.width, daySize.height);
        day.center = CGPointMake(distanceNextYline * i, downToXline + 12);
        [self addSubview:day];
    }
    CGContextStrokePath(context);
    
    //画价格走势曲线
    CGMutablePathRef spadePath = CGPathCreateMutable();
    CGPathMoveToPoint(spadePath, NULL, nextX, downToXline - ([_array[0] doubleValue] - dValue )*coefficient);
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundColor:[UIColor orangeColor]];
    button.layer.cornerRadius=3;
    button.frame=CGRectMake(0, 0, 6, 6);
    button.center=CGPointMake(nextX, downToXline - ([_array[0] doubleValue] - dValue)*coefficient);
    [button setTitle:[NSString stringWithFormat:@"%@",_array[0]] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    //初始显示
    [self clicked:button];
    
    CGFloat lastX = 0.0;
    for (int i = 1; i < [_array count]; i++) {
        
        //计算两个控制点 以及末尾端点
        CGFloat cp1x = nextX+bizieLineAligm;
        CGFloat cp1y = corectPointer1;
        CGFloat cp2x = nextX+distanceNextYline-bizieLineAligm;
        CGFloat cp2y = downToXline - ([_array[i] doubleValue] - dValue)*coefficient;
        CGFloat x = nextX+distanceNextYline;
        CGFloat y = downToXline - ([_array[i] doubleValue]- dValue)*coefficient;
        lastX = x;
        
        CGPathAddCurveToPoint(spadePath, NULL, cp1x, cp1y, cp2x, cp2y, x, y);
//        NSLog(@"POint %d: %f , %f %f %f %f %f",i,cp1x, cp1y, cp2x, cp2y, x, y);
        
        UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
        [button1 setBackgroundColor:[UIColor colorWithRed:255.0f / 255.0f green:123.0f / 255.0f blue:0 alpha:1]];
        button1.layer.cornerRadius=3;
        button1.frame=CGRectMake(0, 0, 6, 6);
        button1.center=CGPointMake(x, y);
        [button1 setTitle:[NSString stringWithFormat:@"%@",_array[i]] forState:UIControlStateNormal];
        [button1 setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button1];
        
        nextX +=distanceNextYline;
        corectPointer1 = downToXline - ([_array[i] doubleValue] - dValue)*coefficient;
    }
    
    [[UIColor colorWithRed:75.0 / 255.0 green:160.0 / 255.0 blue:234.0 / 255.0 alpha:1] set];
    CGContextSetLineWidth(context, 2);
    CGContextAddPath(context, spadePath);
    CGContextStrokePath(context);
    
    //渐变色path
    UIBezierPath* aPath2 = [UIBezierPath bezierPath];
    aPath2.CGPath = spadePath;
    [aPath2 addLineToPoint:CGPointMake(lastX, downToXline)];
    [aPath2 addLineToPoint:CGPointMake(0, downToXline)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = aPath2.CGPath;

    gradient.mask=maskLayer;
}


- (void)clicked:(UIButton *)btn{
    
    [_arrow removeFromSuperview];
    _arrow = [[UIImageView alloc] init];
    _arrow.frame = CGRectMake(0, 0, 15, 15);
    CGPoint center = btn.center;
    center.y -= 15;
    _arrow.center = center;
    _arrow.image = [UIImage imageNamed:@"arrow"];
    [self addSubview:_arrow];
    
    self.money = [NSString stringWithFormat:@"$%@",btn.titleLabel.text];
    if (_delegate && [_delegate respondsToSelector:@selector(btnChange:)]) {
        [_delegate btnChange:self];
    }
    
}

//超出父视图,点击响应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        for (UIView *subView in self.subviews) {
            CGPoint tp = [subView convertPoint:point fromView:self];
            if (CGRectContainsPoint(subView.bounds, tp)) {
                view = subView;
            }
        }
    }
    
    return view;
}

@end

 

posted on 2015-11-09 12:05  墓厄  阅读(452)  评论(0编辑  收藏  举报