iPhone如何使两条连线跟随位置?

时间:2012-04-21 16:08:54

标签: iphone ios core-graphics drawrect

我使用核心图形和

制作了讲话泡泡的尾部

演讲的尾部应该随意移动到任何一点。

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

但是当气泡倒置时,就像气泡部分位于底部,尾部就在上面

顶部,尾部的2条连线相互交叉并形成一个X,当它应该像/ \

可以帮助我吗?

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

1 个答案:

答案 0 :(得分: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,从左上方,中间底部,右上方向上固定,不管你正在绘制哪个方向。

您需要更改它以确保那些-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);

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