2012-04-21 28 views
1

我使用核心图形制作了对话泡泡的尾部,并且使用核心图形和iPhone我如何让两个连接的线跟随到位置?

讲话布雷的尾部应该移动到任何一点,如我所愿。

问题是当尾部的末端部分(尖尖部分)向下时它工作正常。

但是当气泡是上下颠倒,如气泡部分位于底部的尾部分是关于

顶部中尾巴部分的2连接线穿过彼此,形成X,当它应该如/ \

可以someonle请帮助我吗?

double angle(CGPoint p1, CGPoint p2) 
{ 
    //BOOL bRev = TRUE; 
    double dx = p2.x - p1.x; 
    double dy = p2.y - p1.y; 
    if (dx == 0 && dy == 0) return 0.0 

    if (dy == 0) { 
     if (dx > 0) return 0.0; 
     else return M_PI; 
    } 

    if (dx == 0) { 
     if (dy > 0) { 
      double ang = M_PI/2.0; 
      //if (bRev) ang = M_PI*2 - ang; 
      return ang; 
     } 
     else { 
      double ang = M_PI/2.0 * 3; 
      //if (bRev) ang = M_PI*2 - ang; 
      return ang; 
     } 
    } 

    if (dx > 0) { 
     if (dy > 0) { 
      double ang = atan((double)dy/(double)dx); // 1사분면 
      //if (bRev) ang = M_PI*2 - ang; 
      return ang; 
     } 
     else { 
      double ang = atan((double)dy/(double)dx) + 2.0*M_PI; // 4사분면 
      //if (bRev) ang = M_PI*2 - ang; 
      return ang; 
     } 
    } 
    else { 
     double ang = atan((double)dy/(double)dx) + M_PI; 
     //if (bRev) ang = M_PI*2 - ang; 
     return ang; 
    } 
    return 0.0; 
} 

- (double)degree:(CGPoint)p1 and:(CGPoint)p2 
{ 
    double rad = angle(p1, p2); 
    double deg = rad * 180.0/M_PI; 
    if (deg >= 360.0) deg = deg - 360.0; 
    return (deg); 
} 

- (CGPoint)getPoint:(CGPoint)cPt Len:(int)len BaseDegree:(double)d1 MoveDegree:(double)d2 
{ 
    double radian = 3.14/180.0; 
    CGPoint pt1; 
    pt1.x = cPt.x + len*cos((d2-d1)*radian); 
    pt1.y = cPt.y - len*sin((d2-d1)*radian); 
    return pt1; 
} 

- (void)drawRect:(CGRect)rect{ 
    double degree1 = [self degree:CGPointMake(view.frame.origin.x(view.frame.size.width)/2, 
               view.frame.origin.y+(view.frame.size.height)/2) 
           and:lastPt]; 

    CGPoint cpt = CGPointMake(view.frame.origin.x+(view.frame.size.width)/2, 
          view.frame.origin.y+(view.frame.size.height)/2); 

    CGPoint p1 = [self getPoint:cpt Len:10 BaseDegree:degree1 MoveDegree:90]; 
    CGPoint p2 = [self getPoint:cpt Len:10 BaseDegree:degree1 MoveDegree:-90]; 

    CGPathMoveToPoint(ctx, nil, p1.x, p1.y); 
    CGPathAddLineToPoint(ctx, nil, lastPt.x, lastPt.y); 
    CGPathAddLineToPoint(ctx, nil, lastPt.x-2, lastPt.y+2); 
    CGPathAddLineToPoint(ctx, nil, lastPt.x-4, lastPt.y); 
    CGPathAddLineToPoint(ctx, nil, p2.x, p2.y); 
} 

enter image description here

+0

你是什么意思“移动尾巴”? – leftspin 2012-04-21 16:41:24

+0

我修改了我的问题 – tulurira 2012-04-23 01:59:32

回答

1

在你的代码时,您绘制的线,你计算的起始和结束点的权利,但然后在最后你会:

CGPathAddLineToPoint(ctx, nil, lastPt.x, lastPt.y); 
CGPathAddLineToPoint(ctx, nil, lastPt.x-2, lastPt.y+2); 
CGPathAddLineToPoint(ctx, nil, lastPt.x-4, lastPt.y); 

所以你无论你画的是哪个方向,总是在你的泡泡的底部/顶部/侧面上画出小v作为从左上,中下,右上的v的固定正确方式。

您需要更改,以确保这些-2/+ 2来响应你是哪种方式向上/圆 - 例如,当你倒过来,应该最终成为

CGPathAddLineToPoint(ctx, nil, lastPt.x+4, lastPt.y); 
CGPathAddLineToPoint(ctx, nil, lastPt.x+2, lastPt.y-2); 
CGPathAddLineToPoint(ctx, nil, lastPt.x, lastPt.y); 

除它应该被计算,而不仅仅是硬编码。