心电图动画

来源:互联网 发布:中国生物多样性 数据 编辑:程序博客网 时间:2024/04/27 11:01

忘性大,留给自己备份用。


没有使用Objective-C特有的类,所以,任何平台均可通用,都可找到替代类型替换。


依据:贪吃蛇运动原理

#import <UIKit/UIKit.h>#define LINE_LEN 25@interface EGCView : UIView {            UIColor* myColor;    float   x_pos;    float   y_pos;        float   w;    float   h;        float   bei;        float  add_y[25];        float   xx[LINE_LEN];    float   yy[LINE_LEN];            NSTimer* timer;}@property(nonatomic) float x_pos;@end

#import "EGCView.h"@implementation EGCView@synthesize x_pos;- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = [UIColor clearColor];                //Y轴移动循环        float add[25] = {0,0,0,-2,-4,-6,-8,-7,-6,-9,-12,-8,-4,0,4,2,0,0,0,0 ,0,0,0,0,0};                for (int i=0; i<25; i++) {            add_y[i] = add[i];        }                                timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self                                                 selector:@selector(waitingAction)                                                 userInfo:nil repeats:YES]  retain];                        w = frame.size.width;        h = frame.size.height;                bei = h/25.0f;                //初始化可动线的所有点        for (int i=0; i<LINE_LEN; i++) {            xx[i] = 0;            yy[i] = (h-1)/2;                    }                    }    return self;}-(void)waitingAction{    float tmp = self.x_pos;    //X坐标按照倍数移动    if (tmp > w)     {        tmp  = 0.0;    }    else    {        tmp += bei;    }                for (int i=LINE_LEN-1; i>-1; i--) {        if (i == 0) {            xx[0] = x_pos;            yy[0] = (h-1)/2 + (add_y[(((int)(x_pos/(h/25.0)))%25)])*(h/25.0);                    }        else        {            xx[i] = xx[i-1];            yy[i] = yy[i-1];                    }            }            self.x_pos = tmp;    }- (void)drawRect:(CGRect)rect{        CGContextRef context = UIGraphicsGetCurrentContext();        //以下绘制中间的静态的实线
    //设置线宽    CGContextSetLineWidth(context, 2);    //绘制左半段渐变线    int count_1 = w/4+1;        for (int i=0; i<count_1; i++) {                CGContextSetStrokeColorWithColor(context, [[[UIColor grayColor]  colorWithAlphaComponent:((float)( (float)(i*(100.0f/count_1)-1)))/100.0f] CGColor]);                CGContextMoveToPoint(context, i , (h-1)/2 );        CGContextAddLineToPoint(context,  i+1, (h-1)/2  );        CGContextDrawPath(context, kCGPathStroke)   ;    }                CGContextSetStrokeColorWithColor(context, [[UIColor grayColor]  CGColor]);        CGContextMoveToPoint(context, w/4, (h-1)/2 );    CGContextAddLineToPoint(context,  w*3/4, (h-1)/2  );    CGContextDrawPath(context, kCGPathStroke)   ;        //绘制右半段渐变线    int count_2 = w*3/4-1;    for (int i=count_2; i<w+1; i++) {                CGContextSetStrokeColorWithColor(context, [[[UIColor grayColor]  colorWithAlphaComponent:((float)(100.0f - (float)((i-count_2)*(100.0f/(w -count_2))-1)))/100.0f] CGColor]);                CGContextMoveToPoint(context, i , (h-1)/2 );        CGContextAddLineToPoint(context,  i+1, (h-1)/2  );        CGContextDrawPath(context, kCGPathStroke)   ;    }            //以下绘制动态的跳动线    //设置线宽    CGContextSetLineWidth(context, 3);            for (int i = 1; i<LINE_LEN; i++) {                        if(xx[i] < xx[i -1])        {                        CGContextSetStrokeColorWithColor(context, [[[UIColor greenColor] colorWithAlphaComponent:((float)(100.0f - (float)(i*(100/LINE_LEN)-1)))/100.0f] CGColor]);                        CGContextMoveToPoint(context, xx[i] , yy[i] );            CGContextAddLineToPoint(context, xx[i -1],yy[i-1] );                                            CGContextDrawPath(context, kCGPathStroke)   ;        }            }            }- (void)dealloc{    [super dealloc];}//刷新-(void)setX_pos:(float)newX_pos{    x_pos = newX_pos;    [self setNeedsDisplay];        }@end