iPhone我如何使两条连接的线跟随位置? [英] iPhone how do i make two connected line to follow to the position?

查看:61
本文介绍了iPhone我如何使两条连接的线跟随位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用核心图形制作了气泡的尾巴,

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

顶部,尾部的2条连接线彼此交叉并形成一个X,当应该像/ \

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

有人可以帮我吗?

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

推荐答案

在绘制直线时,您在代码中正在正确计算起点和终点,但随后终点:

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

因此,您总是在气泡的底部/顶部/侧面绘制微小的v不管您绘制的是哪种方式,都可以从左上,中间底部,右上角向右向上v。

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.

您需要进行更改,以确保-2 / +2回应您的上/下回合方式-例如,当您倒置时,它最终应为

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.

这篇关于iPhone我如何使两条连接的线跟随位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆