现在的位置: 首页 > 综合 > 正文

绘制带箭头的直线

2014年01月05日 ⁄ 综合 ⁄ 共 799字 ⁄ 字号 评论关闭

从vc移植到objective-c.感觉不错.分享之..

-(void)drawInContext:(CGContextRef)context{ 
    CGContextSetRGBStrokeColor(context,1.0, 0.0,0.0, 1.0);
    CGPoint m_One = CGPointMake(40.0,30.0);
    CGPoint m_Two = CGPointMake(280.0,130.0);
   
   double slopy , cosy , siny;
    double Par =20.0;  //length of Arrow (>)
    slopy = atan2( ( m_One.y - m_Two.y ),
                  ( m_One.x - m_Two.x ) );
    cosy = cos( slopy );
    siny = sin( slopy );//need math.h for these functions
    
    CGContextMoveToPoint(context, m_Two.x, m_Two.y);
    
    CGContextAddLineToPoint( context,m_Two.x + ( Par * cosy - ( Par /2.0 * siny ) ),
                            m_Two.y + ( Par * siny + ( Par /2.0 * cosy ) ) );
    
    CGContextMoveToPoint( context,m_Two.x + ( Par * cosy + Par /2.0 * siny ),
                            m_Two.y - ( Par /2.0 * cosy - Par * siny ) );
    
   CGContextAddLineToPoint(context, m_Two.x, m_Two.y);
   CGContextSetLineWidth(context,2.0);
    CGContextStrokePath(context);
 }

抱歉!评论已关闭.