ios简单的股票分时线

首页 > 科技 > 正文 2021-06-08

发表自话题:股票mid线是什么意思

之前做的项目有需要画类似股票分时线的效果,然后我就开始查资料,最后的结果就是这个

画分时线,我们需要做些什么? (这些都是抄的,作者写的很好,我就用了)

2018-03-13 16_25_13.gif

画框架(网格线);
横坐标上的文字(时间轴);
纵坐标上的文字(左边价格,右边涨跌率);
画分时线;
心跳效果;
添加手势(长按和点击);
画十字交叉线;
画十字交叉线与横纵坐标交点处的文字;

这里是坐标和时间,坐标和价格,的相互转换

/**根据时间获取坐标点*/ -(CGFloat)miniteTimeWithTimeStr:(NSString*)timeStr { if (timeStr == nil || [timeStr isE:@""]) return 0.0; NSArray *temp = [timeStr componentsSeparatedByString:@":"]; NSInteger minte = [temp[0] integerValue]*60 + [temp[1] integerValue]; //每分钟代表的宽度 CGFloat aveWidth = DPW/ 240.0; if (minte DPW *0.5) { totalTime = aveWidth * point.x + 11*60;//下午 } int h = totalTime / 60; int m = ((int)(totalTime + 0.5)) % 60; str = [NSString stringWithFormat:@"%d:%d",h,m]; return str; } /**根据价格获取坐标位置*/ - (CGFloat)priceLabelLocationWithPrice:(CGFloat)price { CGFloat height = DPH - 60; CGFloat avrSpace = height/(self.max - self.min); return height - ((price - self.min)*avrSpace); } /**根据坐标位置获取价格*/ - (NSString*)pricWithPoint:(CGPoint)point { NSString *price = @""; //单位距离代表的价格 CGFloat avePrice = (self.max - self.min)/(DPH - 60); //保留两位小数 price = [NSString stringWithFormat:@"%.2f",(self.max - point.y*avePrice)+0.005]; return price; }

画框架xy轴

/**画框架*/ - (void)drawFramework { UIBezierPath *path = [[UIBezierPath alloc]init]; //三条横线 CGFloat rowSpace = (DPH -60.0)/2.0; NSString *tempStr = @""; for (int i = 0; i < 3; i ++) { [path moveToPoint:CGPointMake(0,rowSpace*i)]; [path addLineToPoint:CGPointMake(DPW, rowSpace*i)]; if (0 == i) { tempStr = [NSString stringWithFormat:@"%.2f",self.max]; }else if (1==i) { tempStr = [NSString stringWithFormat:@"%.2f",self.mid]; }else if (2 == i) { tempStr = [NSString stringWithFormat:@"%.2f",self.min]; } [self drawLabelAtRect:CGRectMake(-25, rowSpace*i-10, 25, 20) textStr:tempStr]; } //4条竖线 CGFloat colSpace = DPW / 4.0; for (int i = 0; i < 5; i ++) { [path moveToPoint:CGPointMake(colSpace*i,0)]; [path addLineToPoint:CGPointMake(colSpace*i, DPH-60)]; if (0 == i) { tempStr = @"9:30"; [self drawLabelAtRect:CGRectMake(colSpace*i, DPH-60 ,30,20) textStr:tempStr]; continue; }else if (1==i) { tempStr = @"10:30"; }else if (2 == i) { tempStr = @"11:30/13:00"; [self drawLabelAtRect:CGRectMake(colSpace*i-30, DPH-60 ,70,20) textStr:tempStr]; continue; }else if (3==i) { tempStr = @"14:00"; }else if (4 == i) { tempStr = @"15:00"; [self drawLabelAtRect:CGRectMake(colSpace*i - 30, DPH-60 ,30,20) textStr:tempStr]; continue; } [self drawLabelAtRect:CGRectMake(colSpace*i-15, DPH-60 ,35,20) textStr:tempStr]; } CAShapeLayer *shapeLayer = [[CAShapeLayer alloc]init]; shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor; shapeLayer.path = path.CGPath; [self.layer addSublayer:shapeLayer]; }

横坐标上的文字(时间轴); 纵坐标上的文字(左边价格,右边涨跌率);

/**画xy轴文字,直接创建一个CATextLayer*/ - (void)drawLabelAtRect:(CGRect)rect textStr:(NSString*)textStr { CATextLayer *textLayer = [CATextLayer layer]; textLayer.frame = rect; [self.layer addSublayer:textLayer]; //set text attributes textLayer.foregroundColor = [UIColor blackColor].CGColor; textLayer.alignmentMode = kCAAlignmentJustified; textLayer.wrapped = YES; //choose a font UIFont *font = [UIFont systemFontOfSize:10]; //set layer font CFStringRef fontName = (__bridge CFStringRef)font.fontName; CGFontRef fontRef = CGFontCreateWithFontName(fontName); textLayer.font = fontRef; textLayer.fontSize = font.pointSize; CGFontRelease(fontRef); textLayer.contentsScale = [UIScreen mainScreen].scale; //choose some text //set layer text textLayer.string = textStr; }

画分时线;

获取纵坐标的最大值,最小值和中间值

说明:(这也是抄的)

横线:分时线最中间的横线对应昨日收盘价,这是固定的.但是,其他横线是根据数据实时变化的,旨在包含所有的数据点.所以,max = mid + 最大偏差;min = mid - 最大偏差;最大偏差=所有数据中偏离mid最大的价格与mid之间差值的绝对值.这样max - min 为价格的波动范围,有了这个就可以根据需要,画出横线(这里画了3条横线);
竖线: 股票交易时间,上午9:30到11:30,下午13:00到15:00,总共4个小时;这里就可以算出单位距离代表的时间,从而确定每个时间节点在横坐标上的位置,画出横坐标.同理,也可算出每分钟代表的长度,这点在画十字交叉线与X轴的交点文字时用的到.对应的方法见上面已抽取的.
分时线: 可以看到在这个方法中已近开始划线了.划线无非就是连线,moveToPoint 和addLineToPoint.为了避免再次遍历所有数据,所以在确定最大和最小价格的循环中就开始添加线;

/**数据处理*/ - (void)preworkForData { //0 拿到纵坐标的3个点(max,mid,min),mid=昨日收盘价 YTTimeLineItem *temp = self.dataArrM[0]; self.mid = [temp.pre_close_px doubleValue]; if ([temp.last_px doubleValue] -[temp.pre_close_px doubleValue] > 0) { self.max = [temp.last_px doubleValue]; self.min = [temp.pre_close_px doubleValue] -([temp.last_px doubleValue] -[temp.pre_close_px doubleValue] ); }else { self.min = [temp.last_px doubleValue]; self.max = [temp.pre_close_px doubleValue] + ([temp.pre_close_px doubleValue] -[temp.last_px doubleValue] ); } for (int i = 0; i< self.dataArrM.count; i ++ ) { YTTimeLineItem *item = self.dataArrM[i]; //获取纵坐标最大,最小,中间值 if (fabs(item.last_px.doubleValue - self.mid) >(self.max - self.mid)) { self.max = self.mid +fabs(item.last_px.doubleValue - self.mid); self.min = self.mid - fabs(item.last_px.doubleValue - self.mid); } //画分时线 CGFloat timeX = [self miniteTimeWithTimeStr:item.curr_time]; CGFloat priceY = [self priceLabelLocationWithPrice:item.last_px.floatValue]; if (i == 0) { if (self.isNeedBackGroundColor) { [self.timeLinePath moveToPoint:CGPointMake(0, DPH - 60)]; [self.timeLinePath addLineToPoint:CGPointMake(timeX, priceY)]; }else { [self.timeLinePath moveToPoint:CGPointMake(timeX, priceY)]; } } if (i == self.dataArrM.count-1) { self.currentPoint = CGPointMake(timeX, priceY); } [self.timeLinePath addLineToPoint:CGPointMake(timeX, priceY)]; } }

心跳效果

/**心跳动画*/ - (void)heartAnimationWithLayer:(CALayer*)layer { CAAnimationGroup *group = [[CAAnimationGroup alloc]init]; group.duration = 0.5; group.repeatCount = HUGE; CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; scaleAnim.toValue = @1.2; CABasicAnimation *alphaAnim = [CABasicAnimation animationWithKeyPath:@"opacity"]; alphaAnim.toValue = @0.3f; group.animations = @[scaleAnim,alphaAnim]; [layer addAnimation:group forKey:nil]; }

添加手势

(长按画十字光标和点击移除十字光标);

/**添加手势*/ - (void)setupGestureRecognize { UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; [self addGestureRecognizer:longPress]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; [self addGestureRecognizer:tap]; } /**长按*/ - (void)longPressAction:(UILongPressGestureRecognizer*)gesture { CGPoint tempPoint = [gesture locationInView:self]; //越界控制 if (tempPoint.x >= DPW) { tempPoint = CGPointMake(DPW, tempPoint.y); } if (tempPoint.x = DPH - 60) { tempPoint = CGPointMake(tempPoint.x, DPH-60); } if (tempPoint.y

标签组:[nsstring

上一篇通达信股市称雄主图指标,通达信缠论完整指标

下一篇爱国者MID:谁说炒股不如炒房?

相关阅读

相同话题文章

推荐内容

热门阅读