iPhone how do i make two connected line to follow

2019-07-23 16:09发布

问题:

I made the tail of speech bubble using core graphic and

The tail part of the speech buble supposed to move to any point as i want.

The problem is when the tail's end part(the pointy part) is downward it works fine.

But when the bubble is upside down, like the bubble part is on the bottom the tail part is on the

top, the tail part's 2 connected line crosses each other and form a X, when it supposed to be like /\

can someonle please help me?

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);
}

回答1:

In your code when you are drawing the line, you are calculating the start and end points right, but then at the end you are going:

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

So you are always drawing the tiny v at the bottom/top/side of your bubble as a fixed right way up v from left top, middle bottom, right top, regardless of which way up you are drawing.

You need to change that to ensure that those -2/+2 respond to which way up / round you are - for instance, when you are upside down, it should end up as

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

except it should be calculated, not just hard coded.